【发布时间】:2017-05-25 14:04:08
【问题描述】:
假设我的测试数据是
db.multiArr.insert({"ID" : "fruit1","Keys" : ["apple", "orange", "banana"]})
db.multiArr.insert({"ID" : "fruit2","Keys" : ["apple", "carrot", "banana"]})
为了得到像胡萝卜这样的个别水果
db.multiArr.find({'Keys':{$in:['carrot']}})
当我对橙子和香蕉进行 or 查询时,我看到记录fruit1 和fruit2
db.multiArr.find({ $or: [{'Keys':{$in:['carrot']}}, {'Keys':{$in:['banana']}}]})
输出的结果应该是fruit2,然后是fruit1,因为fruit2既有胡萝卜又有香蕉
【问题讨论】:
-
i see both the records fruit1 and then fruit2- 然后你说should be fruit2 and then fruit1- 你得到的正是你想要的?! -
你想做$and查询吗?
-
@Alex 问题还说" ...输出应该是fruit2然后是fruit1,因为fruit2既有胡萝卜也有香蕉"。这就是要求对结果进行“加权排序”的关键,而不仅仅是返回两个文档,因为它们都匹配。
-
啊,我明白了
标签: mongodb mongodb-query aggregation-framework