繁体   English   中英

MongoDB:如何使用特定规则查询$ in中的特定元素?

[英]MongoDB: how to query with a specific rule for a specific element in $in?

老实说,我怀疑是否有可能,但只在这里试一下。

这是数据库记录的示例

{
    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”中的所有记录。

我想查询“水果”,“蔬菜”,“肉”以及'fruit' only but not others数量大于5的'fruit' only but not others记录, 'fruit' only but not others 因此结果将是

orange, tomato, beef

不确定我的解释是否有意义,但这是可能的查询吗? 还是我应该查询两次? 谢谢阅读。

请试试:

{ 
    $or: [
        { type: { $in: ['vegetable', 'meat'] } },
        { type: { $in: ['fruit'] }, quantity: { $gt: 5 } } 
    ]
}

希望对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM