0

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

1
  • How do you filter these records, according to which field and which value? Commented Feb 26, 2017 at 19:44

1 Answer 1

0

You can use $in operator to match a particular field agains an array of value.

db.posts.find({author: {$in: ["annT","ahn","ty"]}})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.