"is not null" 的查询在 MongoDB 中不起作用
Query for "is not null" not working in MongoDB
我正在使用 mongodb table,但在获取某些字段中值不同于 null 的文档时遇到问题。
我假设我使用 $ne 运算符是错误的,但是,鉴于此文档:
{
"createdAt" : "2022-04-07T13:18:18Z",
"expired" : false,
"responses": [
{
"id": "header",
"options": [
{
"id": "1",
"text": ""
}
],
"textForSearch": null
},
{
"id": "content",
"options": [
{
"id": "content test 01",
"text": "content test 01"
},
{
"id": "content test 02",
"text": "content test 02"
}
],
"title": "test title",
"questionType": "selection",
"textForSearch": null
},
{
"id": "footer",
"options": [
{
"id": "negative",
"text": "Negative"
},
{
"id": "positive",
"text": "Positive"
}
],
"title": "test title",
"questionType": "selection",
"textForSearch": null
},
{
"id": "test_id",
"options": null,
"title": "",
"questionType": "text",
"textForSearch": "comentario de test"
}
]
}
以下查询:
db.getCollection('table').find({"responses.textForSearch": {$ne: null} })
结果:
Fetched 0 record(s) in 1ms.
我错过了什么?
使用 $elemMatch
运算符。
您可以参考 Single Query Condition 以查看使用 dot notation
和 $elemMatch
与 $not
或 $ne
运算符一起使用时产生不同结果的示例.
db.collection.find({
"responses": {
"$elemMatch": {
"textForSearch": {
$ne: null
}
}
}
})
我正在使用 mongodb table,但在获取某些字段中值不同于 null 的文档时遇到问题。 我假设我使用 $ne 运算符是错误的,但是,鉴于此文档:
{
"createdAt" : "2022-04-07T13:18:18Z",
"expired" : false,
"responses": [
{
"id": "header",
"options": [
{
"id": "1",
"text": ""
}
],
"textForSearch": null
},
{
"id": "content",
"options": [
{
"id": "content test 01",
"text": "content test 01"
},
{
"id": "content test 02",
"text": "content test 02"
}
],
"title": "test title",
"questionType": "selection",
"textForSearch": null
},
{
"id": "footer",
"options": [
{
"id": "negative",
"text": "Negative"
},
{
"id": "positive",
"text": "Positive"
}
],
"title": "test title",
"questionType": "selection",
"textForSearch": null
},
{
"id": "test_id",
"options": null,
"title": "",
"questionType": "text",
"textForSearch": "comentario de test"
}
]
}
以下查询:
db.getCollection('table').find({"responses.textForSearch": {$ne: null} })
结果:
Fetched 0 record(s) in 1ms.
我错过了什么?
使用 $elemMatch
运算符。
您可以参考 Single Query Condition 以查看使用 dot notation
和 $elemMatch
与 $not
或 $ne
运算符一起使用时产生不同结果的示例.
db.collection.find({
"responses": {
"$elemMatch": {
"textForSearch": {
$ne: null
}
}
}
})