How to update a collection based on another collection without joins? - mongodb

I have a collection that looks like:
transactions
{
"_id": ...,
"status": "new"
}
processed
{
"_id": id value from collection above
}
So I want to update the transaction collection status value to "processed", if there is record in the processed collection.
The processed id value is from the transactions collection id.
Is this possible to do in mongo console or I have to do this using code?

You can do that with a $merge stage in aggregation.
remove all fields except the _id
add any fields with the updated value
merge with the other collection
db.procecessed.aggregate([
{$project:{_id:1}},
{$addFields:{status:"processed"}},
{$merge:"transactions"}
])

Related

How to trim a value by a particular length and then apply lookUp in mongoDB

The main issue i'm facing is doing multi document joins. For example, when I try and run a query on orders by customer I can't find an easy way to join orders to customers on a customer id because in the orders object the customer id has _user$ appended to the beginning of the user ID and I don't know how to truncate that in an aggregation pipeline.
eg; In the order object, the customer is defined as _p_customer:"_User$8qXjk40eOd"
But in the user table the _id is just 8qXjk40eOd and therfore the default lookup function in charts cannot match the two.
Note: I'm using parse server and it stores pointers in the above mentioned way.
This is how the data is being stored in the mongoDB order collection.
customer field in the above image is the pointer to the _User collection
This is how the data is stored in the mongoDB _User collection.
My requirement is to write mongo query that find all users and their related orders count. I'm not able to do that because I don't how to remove _User$ from the order row and join it the _User via _id.
before lookup in aggregate substr the field you want to use in $lookup
db.collection.aggregate([
{
$addFields :{
nUser : { $substr: [ "$_p_customer", 6, -1 ] } // _User$ has 6 characters
}
},
{
$lookup :{
from:"usersCollection",
localField:"nUser",
foreignField : "_id",
as : "UserObject"
}
}
])

Mongoose Updating an array in multiple documents by passing an array of filters to update query

I have multiple documents(3 documents in this example) in one collection that looks like this:
{
_id:123,
bizs:[{_id:'',name:'a'},{_id:'',name:'b'}]
},
{
_id:456,
bizs:[{_id:'',name:'e'},{_id:'',name:'f'}]
}
{
_id:789,
bizs:[{_id:'',name:'x'},{_id:'',name:'y'}]
}
Now, I want to update the bizs subdocument by matching with my array of ids.
That is to say, my array filter for update query is [123,789], which will match against the _id fields of each document.
I have tried using findByIdAndUpdate() but that doesn't allow an array for the update query
How can I update the 2 matching documents (like my example above) without having to put findByIdAndUpdate inside a forloop to match the array element with the _id?
You can not use findByIdAndUpdate when updating multiple documents, findByIdAndUpdate is from mongoose which is a wrapper to native MongoDB's findOneAndUpdate. When you pass a single string as a filter to findByIdAndUpdate like : Collection.findByIdAndUpdate({'5e179dac627ef7823643cd97'}, {}) - then mongoose will internally convert string to ObjectId() & form it as a filter like :_id : ObjectId('5e179dac627ef7823643cd97') to execute findOneAndUpdate. So it means you can only update one document at a time, So if you've multiple documents to be updated use update with option {multi : true} or updateMany.
Assume if you wanted to push a new object to bizs, this is how query looks like :
collection.updateMany({ _id: { $in: [123, 456] } }, {
$push: {
bizs: {
"_id": "",
"name": "new"
}
}
})
Note : Update operations doesn't return the documents in response rather they will return write result which has information about n docs matched & n docs modified.

MongoDB - Querying a field in a lower level of document

Issue with fetching the data of a particular field within a document
The followoing is my collection structure
colleciton mail
mailtype
criteria
triggerinfor
code ----> multiple codes are there.
description
status
Tried this but not working
db.getCollection('Mail').find(
{
"MailType" :"Printed",
"MailName" :"Welcomemail",
"programType":"Maile",
"criterias" :
{"triggerInformation":
{"code" :"MAIL007"},
{"description":"1234 Welcome maile"}
}
}
)
I have to retrieve the document with code MAIL007 only.. could some one help in getting a query for that
There are two ways to do it, depending on what you need:
1- Matching an exact nested document: this way, you are querying for documents that have a nested document that is exactly like the one in your search criteria.
db.getCollection('Mail').find(
{
"MailType" :"Printed",
"MailName" :"Welcomemail",
"programType":"Maile",
"criterias.triggerInformation": {
"code": "MAIL007",
"description": "1234 Welcome maile",
"status": "some status"
}
}
Notice that you need to explicitly inform a value to each of the nested document fields in your search criteria. If any field is missing the query will return nothing.
2- Querying a field inside a nested document: this way, you are querying for documents based on the value of one or more fields inside a nested document.
db.getCollection('Mail').find({
"MailType":"Printed",
"MailName":"Welcomemail",
"programType":"Maile",
$and: [
{"criterias.triggerInformation.code": "MAIL007"},
{"criterias.triggerInformation.description": "description":"1234 Welcome maile"}
]
})
In the code above you are querying for documents based on the values of the fields code and description inside the nested document criterias.triggerInformation.
You can find more info about querying nested documents at MongoDB's docs here

Mongo filter results by distinct field

I use this ruby driver for mongo : https://github.com/mongodb/mongo-ruby-driver
I have many documents in a collection like this :
{
"_productId" => 10
....
}
{
"_productId" => 10
....
}
{
"_productId" => 12
....
}
What is the request to get all documents with a distinct "_productId", I mean I want to take the first and the last collection item in this exemple.
#db['collection'].distinct('_productId')
http://docs.mongodb.org/manual/reference/method/db.collection.distinct/
http://wiki.summercode.com/mongodb_aggregation_functions_and_ruby_counting_and_grouping
With the basis of selecting a document from a group being unknown, and assuming the first document for each productid to be selected is based on their insertion order, you can use the aggregation $group,$project operators together.
Group all the records based on their productId, choose the first document from each group.
db.collection.aggregate([
{$group:{"_id":"$_productId","distinct_doc":{$first:"$$ROOT"}}},
{$project:{"distinct_doc":1,"_id":0}}
])
If you have an _id field for each document, the first document would be the one which was inserted first for that particular group. If you choose to do it based on some other field, then you would need to perform an additional $sort operation before you group the records.
for example,
db.collection.aggregate([
{$sort:{"someField":1}},
{$group:{"_id":"$_productId","distinct_doc":{$first:"$$ROOT"}}},
{$project:{"distinct_doc":1,"_id":0}}
])
In this case the first document per group, would be the one which comes top when sorted.

Why is my MongoDb query inserting an embedded document on Update?

This is my MongoDB query:
db.events.update({date:{$gte: ISODate("2014-09-01T00:00:00Z")}},{$set:{"artists.$.soundcloud_toggle":false}},{multi:true,upsert:false})
Apparently I cannot use "artists.$.soundcloud_toggle" to update all artist documents within the artists array:
"The $ operator can update the first array element that matches
multiple query criteria specified with the $elemMatch() operator.
http://docs.mongodb.org/manual/reference/operator/update/positional/"
I'm happy to run the query a number of times changing the index of the array in order to set the soundcloud_toggle property of every artist in every event that matches the query e.g
artists.0.soundcloud_toggle
artists.1.soundcloud_toggle
artists.2.soundcloud_toggle
artists.3.soundcloud_toggle
The problem is: when there is say, only one artist document in the artists array and I run the query with "artists.1.soundcloud_toggle" It will insert an artist document into the artist array with a single property:
{
"soundcloud_toggle" : true
},
(I have declared "upsert:false", which should be false by default anyways)
How do I stop the query from inserting a document and setting soundcloud_toggle:false when there is no existing document there? I only want it to update the property if an artist exists at the given artists array index.
If, like you said, you don't mind completing the operation with multiple queries, you can add an $exists condition to your filter.
E.g. in the 5th iteration, when updating index=4, add: "artists.4": {$exists: true}, like:
db.events.update(
{ date: {$gte: ISODate("2014-09-01T00:00:00Z")},
"artists.4": {$exists: true} },
{ $set:{ "artists.4.soundcloud_toggle" :false } },
{ multi: true, upsert: false }
)