我想检查一个字段是否不存在于一个对象数组中。假设我在文档中有一个名为attributes的数组:
[
{
attributes: [
{
name: "Cool",
value: true
}
]
}
]我希望找到一些没有具体说明的项目。我将使用$or运算符查找空值。
$attributes: {
$elemMatch: {
$or: [
{ name: 'cool', value: '' },
{ name: 'cool', value: { $exists: false } },
{ name: {ne: 'cool' } ?????
]
}
}但是我想找到{name:'Cool'}不在数组中并且无法确定语法的项。
任何帮助都会很好,非常感谢
发布于 2022-09-12 05:29:17
简单的$ne使用在$elemMatch表达式之外就足够了,Mongo“扁平”数组用于大多数查询,因此如果数组中的单个元素与查询匹配,就足以满足条件。
在您的情况下,如果使用$ne和cool是元素之一,那么条件将被“匹配”,文档被排除在外,如下所示:
db.collection.find({
"attributes.name": {
$ne: "Cool"
}
})https://stackoverflow.com/questions/73683048
复制相似问题