Reputation: 35
How to get objects from a collection by using match with multiple values of an array.For ex: I have below data in a collection
{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 } { "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 } { "_id" : ObjectId("55f5a192d4bede9ac365b257"), "author" : "ahn", "score" : 60, "views" : 1000 } { "_id" : ObjectId("55f5a192d4bede9ac365b258"), "author" : "li", "score" : 55, "views" : 5000 } { "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), "author" : "annT", "score" : 60, "views" : 50 } { "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), "author" : "li", "score" : 94, "views" : 999 } { "_id" : ObjectId("55f5a1d3d4bede9ac365b25b"), "author" : "ty", "score" : 95, "views" : 1000 }Now i get an array as input
{"annT","ahn","ty"}can it do something like this to get all the records with these authors
db.articles.aggregate( [ { $match : { author : {"annT","ahn","ty"} } } ] );This input array length varies according to user selection.Is there any better way to get this records? ThanYou in advance
Upvotes: 0
Views: 1565
Reputation: 7920
You can use $in
operator to match a particular field agains an array of value.
db.posts.find({author: {$in: ["annT","ahn","ty"]}})
Upvotes: 0