mongodb - Mongoose.find() 如何查询属性大于某个数字或值为空或属性不存在的文档?

标签 mongodb mongoose model find

我有这样的文件:

{
   "name": "John",
   "age" : 10
},
{
   "name": "Jack",
   "age" : 20
},
{
   "name": "Tom",
   "age" : 30
},
{
   "name": "Lisa"
}

如何构造查询.find()来查询年龄的 Mongoose 模型。

  • 年龄大于:13
  • 年龄小于:27

或者

  • 年龄属性为空

或者

  • 年龄属性不存在?

非常感谢您的回复!

最佳答案

尝试$或条件,

db.collection.find({
  $or: [
    { age: { $gt: 13, $lt: 27 } },
    { age: null },
    { age: { $exists: false } }
  ]
})

Playground

关于mongodb - Mongoose.find() 如何查询属性大于某个数字或值为空或属性不存在的文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67124581/

相关文章:

MongoDB,mongorestore : "Can only replay oplog on full restore"

mongodb - 对我的案例MongoDB模式设计的看法

node.js - NodeJS 类导入

android - 谁在 mvp android 中调用 db 查询和 http get

performance - MongoDB 查找和删除 - 最快的方法

mongoose - 使用 SCRAM-SHA-1 在 Mongoose 中进行身份验证

node.js - mongoose document.validate 从 4.4x 更新到 ^4.5.1 后抛出 RangeError

javascript - 从 Mongodb 中检索一个元素

cakephp - 如何从编辑或创建中判断我是否处于 beforeSave 状态?蛋糕PHP

mysql - 在本地开发环境中配置 Django 以使用导入的 MySQL 数据库