I need a find query, which is searching for an existing attribute in an array.
If this is the document...
document
{
id: '123',
attr: 'anything',
author: [
{ id: '1' },
{ id: '2' }
]
}
... I want to check if there is a document with an author id existing:
find query
var id = '123';
var uid = '2';
Collection.find({ _id: id, author: { id: uid } })
This should give me one result.
If uid = '3'
there would be no result.
Use "dot notation" to denote embedded fields:
Collection.find({ "_id": id, "author.id": uid })
Otherwise you are asking for a document that has "exactly" and only a single object with the requested value. This just asks "does one possible value match".
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.