I have a collection with documents in a MongoDB like this:
{
"recipe_id": "123",
"recipe_name": "Pizza",
"recipe_author": "Jane Doe"
{
Then, in JavaScript, I have an array of objects like this:
[{
"author": "John Doe"
}
{
"author": "Jane Doe"
}]
I want to find at least one document where the recipe_author field in MongoDB matches the author field in my JavaScript array of objects, so I can get the author_id.
If my authors were in an array, I could check for them all at once like this:
["Jane Doe", "Billy Bob", "Another Author"]
collection.findOne({
'author_name': { $in: authors }
}
But, they are an array of objects. Is it possible to do something along these lines?
collection.findOne({ 'author_name': { $in: <each author field in my object> } }
I think iterating author inside $in construct may not be possible , but prior querying required array can be formed .
var author = [{
"author": "John Doe"
}
{
"author": "Jane Doe"
}];
var authorObj = author.map(function(eachObj){
return eachObj.author;
});
collection.findOne({
'author_name': { $in: authorObj }
}
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.