mongodb - 为什么mongodb会找到这个文档($ne: null for array)
问题描述
数据:
db.inventory.insertMany([
{ _id: 1, item: null },
{ _id: 2 },
{ _id: 3, item: 3 },
{ _id: 4 items: [1, 2, 3] },
{ _id: 5, items: [] }
])
查询一:
db.inventory.find({ 'item': {$ne: null} })
结果1:
{ _id: 3, item: 3 }
查询 2:
db.inventory.find({ 'items.0': {$ne: null} })
结果 2:
{ _id: 3, items: [1, 2, 3] },
{ _id: 4, items: [] }
为什么 mongoDB 会找到这个文档:{ _id: 4, items: [] }?这是一个错误吗?
解决方案
Use $exists: true
instead of $ne: null
.
$ne docs explain:
$ne selects the documents where the value of the field is not equal to the specified value. This includes documents that do not contain the field.
推荐阅读
- android - 如何在画布上绘制像素动画?
- python - Django 通过文件名运行测试
- amazon-web-services - 无法连接到 AWS RDS
- node.js - Playwright + Firefox:如何禁用下载提示并默认保存?
- c - GTK+ 应用程序在 Windows 10 上看起来坏了,但只是有时
- c - C VS VB 中的比较
- json - 使用 PowerShell 循环确定对象和/或 JSON 值的深度
- json - 无法在 Slim 框架上的 PHPUnit 中提交 JSON 请求
- segmentation-fault - 我简化了项目的一部分以解决我遇到的这个特定问题(大小为 4 的无效写入/分段错误)
- rust - 通过示例了解绝对和相对路径的 Rust 组件迭代器