如何通过使用与数组的多个值匹配来从集合中获取对象。例如:我在集合中有以下数据
{ "_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 }现在我得到一个数组作为输入{{2}它可以做这样的事情来获取这些作者的所有记录
{"annT","ahn","ty"}这个输入数组长度根据用户选择而变化。有没有更好的方法来获取这些记录?比你提前
答案 0 :(得分:0)
您可以使用$in
运算符将特定字段再次匹配一个值数组。
db.posts.find({author: {$in: ["annT","ahn","ty"]}})