老实说,我怀疑这是否可能,但只是在这里尝试一下。
这是数据库记录的示例
{
type: 'fruit',
name: 'apple',
quantity: 3
}
{
type: 'fruit',
name: 'orange',
quantity: 10
}
{
type: 'vegetable',
name: 'tomato',
quantity: 4
}
{
type: 'meat',
name: 'beef',
quantity: 2
}
然后这是查询
{type: { $in: ['fruit', 'vegetable', 'meat']}}
因此,这将返回集合“apple”、“orange”、“tomato”、“beef”中的所有记录。
我想查询“水果”、“蔬菜”、“肉类”以及仅查询“水果”数量超过 5 的记录,不查询其他记录
。
所以结果将是
orange, tomato, beef
不确定我的解释是否有意义,但这是否是一个可能的查询?或者我应该去查询两次? 感谢您的阅读。
最佳答案
请尝试:
{
$or: [
{ type: { $in: ['vegetable', 'meat'] } },
{ type: { $in: ['fruit'] }, quantity: { $gt: 5 } }
]
}
希望对您有所帮助。
关于javascript - MongoDB:如何使用特定规则查询$in中的特定元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27954457/