I have this kind of model:
MyCollection
{
...
groups : [{ type: Schema.Types.ObjectId, ref: 'Group' }],
...
}
Now how can I find all documents which have a group with known _id? I tried
MyCollection.find({
'groups' : {
$elemMatch : {
'$ref' : 'Group',
'$id' : myid
}
}
}).exec(cb);
but it doesn't work. I am really frustrated with this question.
Sample document:
{ _id: 52d7dd87f3f1e72c7c000005,
groups: [ { _id: 52d02565360c206933000013, name: 'groupname' } ],
date: Thu Jan 16 2014 10:15:00 GMT+0200 (EET),
...
}
Another try:
> db.groups.find({ "name" : "NINFOS13"})
{ "_id" : ObjectId("52d8fad69c7817b52a000012"), "createdAt" : ISODate("2014-01- 17T09:41:42.365Z"), "name" : "NINFOS13", "__v" : 0 }
> db.subjects.insert({groups : [ { _id : ObjectId("52d8fad69c7817b52a000012"), name : "NINFOS13"}]})
> db.subjects.find()
{ "_id" : ObjectId("52d8fb7c1c4493a980630c68"), "groups" : [ { "_id" : ObjectId("52d8fad69c7817b52a000012"), "name" : "NINFOS13" } ] }
> db.subjects.find({"groups._id" : ObjectId("52d8fb7c1c4493a980630c68")}).count()
0
Sorry, I made a mistake this query works!
Works for me (using mongo shell):
> db.stack.insert({groups: [ { _id: ObjectId("52d02565360c206933000013"), name: "groupname" } ]});
> db.stack.find()
{ "_id" : ObjectId("52d8924b5c90ea648f2a4664"), "groups" : [ { "_id" : ObjectId("52d02565360c206933000013"), "name" : "groupname" } ] }
> db.stack.find({"groups._id": ObjectId("52d02565360c206933000013")});
{ "_id" : ObjectId("52d8924b5c90ea648f2a4664"), "groups" : [ { "_id" : ObjectId("52d02565360c206933000013"), "name" : "groupname" } ] }
Perhaps you have a typo.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.