How do I update an array on a nested subdocument in Meteor? - mongodb

I have a collection like this (called project):
{
"_id" : id,
name: '',
title: '',
"user": [
{
"username" : "manager",
"role" : [
"manager"
],
"group" : "group",
"Assign" : [
"education"
]
},
],
"category" : [
"education"
]
}
I'm trying to update the collection to replace the "Assign" array items:
Project.update({
_id: id,
'user.Assign': cat
}, {
$set: {
'user.$.Assign.$': new Cat
}
}, {
multi: true
})
I get the
Cannot apply the positional operator without a corresponding query field containing an array
error though. What is the proper way to do this in MongoDB?

It doesn't look like you need that last positional operator. You may want to use something like $push on that simple array. Try changing the $set in your update statement to something like the following:
$push: {
'user.$.Assign' : "new Cat"
}
Of course, you could use other array operators here if appropriate, such as $addToSet, etc.

Related

MongoDB - Update a field in an object of an array with multiple filter conditions

If I have the following document in my database :
{
"_id" : MainId,
"subdoc" : [
{
"_id" : SubdocId,
"userid" : "someid",
"toupdate": false,
},
{
"_id" : SubdocId2,
"userid" : "someid2",
"toupdate": false,
}
],
"extra" : [
"extraid1",
"extraid2"
]
}
How can I update the subdocument SubdocId2 where the id (SubdocId2) must match and either SubdocId2's userid is "someid2" OR value "extraid1" exists in "extra"?
The farthest I got is:
db.coll.update({
"subdoc._id":"SubdocId2", {
$or: ["extra":{$in:["extraid1"]}, "subdoc.userid":"someid2"]
}
}, {"subdoc.$.toupdate":true})
Maybe I forgot to quote up something, but I get an error (SyntaxError: invalid property id)
Please try this :
db.coll.update(
{
$and: [{ "subdoc._id": SubdocId2 }, {
$or: [{ "extra": { $in: ["extraid1"] } },
{ "subdoc.userid": "someid2" }]
}] // subdoc._id && (extra || subdoc.userid)
}, { $set: { "subdoc.$.toupdate": true } })
In your query there are couple of syntax issues & also if you don't use $set in your update part - it replace the entire document with "subdoc.$.toupdate": true. Of course if you're using mongoose that's different scenario as mongoose will internally does add $set but when executing in shell or any client you need to specify $set. Also if SubdocId2 is ObjectId() you need to convert string to ObjectId() in code before querying database.

Project values of different columns into one field

{
"_id" : ObjectId("5ae84dd87f5b72618ba7a669"),
"main_sub" : "MATHS",
"reporting" : [
{
"teacher" : "ABC"
}
],
"subs" : [
{
"sub" : "GEOMETRIC",
"teacher" : "XYZ",
}
]
}
{
"_id" : ObjectId("5ae84dd87f5b72618ba7a669"),
"main_sub" : "SOCIAL SCIENCE",
"reporting" : [
{
"teacher" : "XYZ"
}
],
"subs" : [
{
"sub" : "CIVIL",
"teacher" : "ABC",
}
]
}
I have simplified the structure of the documents that i have.
The basic structure is that I have a parent subject with an array of reporting teachers and an array of sub-subjects(each having a teacher)
I now want to extract all the subject(parent/sub-subjects) along with the condition if they are sub-subjects or not which are taught by a particular teacher.
For eg:
for teacher ABC i want the following structure:
[{'subject':'MATHS', 'is_parent':'True'}, {'subject':'CIVIL', 'is_parent':'FALSE'}]
-- What is the most efficient query possible ..? I have tried $project with $cond and $switch but in both the cases I have had to repeat the conditional statement for 'subject' and 'is_parent'
-- Is it advised to do the computation in a query or should I get the data dump and then modify the structure in the server code? AS in, I could $unwind and get a mapping of the parent subjects with each sub-subject and then do a for loop.
I have tried
db.collection.aggregate(
{$unwind:'$reporting'},
{$project:{
'result':{$cond:[
{$eq:['ABC', '$reporting.teacher']},
"$main_sub",
"$subs.sub"]}
}}
)
then I realised that even if i transform the else part into another query for the sub-subjects I will have to write the exact same thing for the property of is_parent
You have 2 arrays, so you need to unwind both - the reporting and the subs.
After that stage each document will have at most 1 parent teacher-subj and at most 1 sub teacher-subj pairs.
You need to unwind them again to have a single teacher-subj per document, and it's where you define whether it is parent or not.
Then you can group by teacher. No need for $conds, $filters, or $facets. E.g.:
db.collection.aggregate([
{ $unwind: "$reporting" },
{ $unwind: "$subs" },
{ $project: {
teachers: [
{ teacher: "$reporting.teacher", sub: "$main_sub", is_parent: true },
{ teacher: "$subs.teacher", sub: "$subs.sub", is_parent: false }
]
} },
{ $unwind: "$teachers" },
{ $group: {
_id: "$teachers.teacher",
subs: { $push: {
subject: "$teachers.sub",
is_parent: "$teachers.is_parent"
} }
} }
])

MongoDB, how to query a document but limit an array in that document

I have the following document:
{
_id: asdfasdf,
title: "ParentA",
children: [
{
_id: abcd <-- using new ObjectId() to generate these on creation
title: "ChildA",
},
{
_id: efgh,
title: "ChildB"
}
]
}
What I want to do is use findOne but I only want the returned document to contain a single child in its array.
Sudo logic
Categories.findOne({ _id: "asdfasdf" }, { children: _Id: "abcd" });
I want the returned document to look like this:
{
_id: asdfasdf,
title: "ParentA",
children: [
{
_id: abcd <-- using new ObjectId() to generate these on creation
title: "ChildA",
}
]
}
The purpose of this is so I can pass the information into an edit form, and then update that single child object in the array on save.
I'm getting confused as to how to limit the result set.
Thank you very much!
---- Edit ----
After attempting to use the suggested duplicate question as a reference, I'm getting undefined in my results. I really want to use findOne instead of find() as well. On the client, a collection object, even though it contains one item, is treated differently than a single (findOne) object that is returned.
Here is what I've tried.
db.Category.findOne({
"_id": parentid,
"children._id": childid
},{
"_id": childid,
"children": {
"$elemMatch": {
"_id": childid
}
}
});
db.Category.findOne({
"_id": parentid
},{
"children": {
"$elemMatch": {
"_id": childid
}
}
});
I've tried several more variations like the above.
---- Edit 2 ----
Based on a comment, here is the output of the following query:
db.category.findOne({ "_id" : "9dYgKFczgiRcNouij"});
{
"title" : "Appliances",
"active" : true,
"children" : [
{
"_id" : ObjectId("680d55c6995ef6f0748278c2"),
"title" : "Laundry",
"active" : true
},
{
"_id" : ObjectId("2b4469c1a4c8e086942a1233"),
"title" : "Kitchen"
"active" : true
},
{
"_id" : ObjectId("4f5562ef7668839704c851d6"),
"title" : "Other"
"active" : true
}
],
"_id" : "9dYgKFczgiRcNouij"
}
So I think perhaps my problem is how I created the children._id in the array. I used new ObjectId() to generate the _id.
--- Edit 3 ---
db.category.findOne({
"_id": "9dYgKFczgiRcNouij"
},{
"children": {
"$elemMatch": {
"_id": ObjectId("4f5562ef7668839704c851d6")
}
}
});
This returns ObjectID is not defined.

MongoDB: select matched elements of subcollection

I'm using mongoose.js to do queries to mongodb, but I think my problem is not specific to mongoose.js.
Say I have only one record in the collection:
var album = new Album({
tracks: [{
title: 'track0',
language: 'en',
},{
title: 'track1',
language: 'en',
},{
title: 'track2',
language: 'es',
}]
})
I want to select all tracks with language field equal to 'en', so I tried two variants:
Album.find({'tracks.language':'en'}, {'tracks.$': 1}, function(err, albums){
and tied to to the same thing with $elemMatch projection:
Album.find({}, {tracks: {$elemMatch: {'language': 'en'}}}, function(err, albums){
in either case I've got the same result:
{tracks:[{title: 'track0', language: 'en'}]}
selected album.tracks contain only ONE track element with title 'track0' (but there should be both 'track0', 'track1'):
{tracks:[{title: 'track0', language: 'en'}, {title: 'track1', language: 'en'}]}
What am I doing wrong?
Like #JohnnyHK already said, you'll have to use the aggregation framework to accomplish that because both $ and $elemMatch only return the first match.
Here's how:
db.Album.aggregate(
// This is optional. It might make your query faster if you have
// many albums that don't have any English tracks. Take a larger
// collection and measure the difference. YMMV.
{ $match: {tracks: {$elemMatch: {'language': 'en'}} } },
// This will create an 'intermediate' document for each track
{ $unwind : "$tracks" },
// Now filter out the documents that don't contain an English track
// Note: at this point, documents' 'tracks' element is not an array
{ $match: { "tracks.language" : "en" } },
// Re-group so the output documents have the same structure, ie.
// make tracks a subdocument / array again
{ $group : { _id : "$_id", tracks : { $addToSet : "$tracks" } }}
);
You might want to try that aggregate query with only the first expression and then add expressions line by line to see how the output is changed. It's particularly important to understand how $unwind creates intermediate documents that are later re-merged using $group and $addToSet.
Results:
> db.Album.aggregate(
{ $match: {tracks: {$elemMatch: {'language': 'en'}} } },
{ $unwind : "$tracks" },
{ $match: { "tracks.language" : "en" } },
{ $group : { _id : "$_id", tracks : { $addToSet : "$tracks" } }} );
{
"result" : [
{
"_id" : ObjectId("514217b1c99766f4d210c20b"),
"tracks" : [
{
"title" : "track1",
"language" : "en"
},
{
"title" : "track0",
"language" : "en"
}
]
}
],
"ok" : 1
}

Complex Logic quires on simple mongo document

I am battling with forming complex logic queries on very basic data types in mongo. Essentially I can have millions of user attributes so my basic mongo document is:
{
name: "Gender"
value: "Male"
userId : "ABC123"
}
{
name: "M-Spike"
value: 0.123
userId : "ABC123"
}
What I would like to do is search for things like findAll userId where {name : "Gender, value: "Male"} AND { name : "m-spike", value : { $gt : 0.1} }
I have tried using the aggregation framework but the complexity of the queries is limited, basically I was ORing all the criteria and counting the results by sampleId (which replicated a rudimentary AND)
I can see a way to do it for N being the number of attributes you want to query about (N being 2 in your example). Try something like this:
db.collection.aggregate(
[ { $match: {$or: [
{"name":"M-Spike","value":{$gt:.1}},
{"name":"Gender","value":"Male"}
]
}
},
{ $group: { _id:"$userId",total:{$sum:1}}
},
{ $project: { _id:1,
matchedAttr : { $eq: ["$total",2] }
}
}
]
)
You will get back:
{
"result" : [
{
"_id" : "XYZ123",
"matchedAttr" : false
},
{
"_id" : "ABC123",
"matchedAttr" : true
}
],
"ok" : 1
}
Now, if you had 2 conditions you matched via "$or" then you get back true for _id that matched both of them. So for five conditions, your $match: $or array will have five conditional pairs, and the last $project transformation will be $eq: ["$total",5]
Built in to this solution is the assumption that you cannot have duplicate entries (i.e. _id cannot have "M-Spike":.5 and also "M-Spike":.2. If you can, then this won't work.