欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Mongoose find

程序员文章站 2022-07-12 14:35:11
...

Mongodb find

model: Document

  1. 查询单个字段
1. 未删除的
Document.find({deleted: {$ne: true} 或者 deleted: false})

2. 存在的Document
Document.find({title:{$exists:true})
Document.find({title:{$nin:[null,undefined,false]})

3. 不存在的Document
Document.find({title:{$in:[null,undefined,false]})
  1. 查询多个字段
1. title为lidan 或者 age为50的document
Document.find({ $or: [{title: 'lidan'}, {age: 50}]})

2. title为lidan 而且 age为50的document
Document.find({{title: 'lidan'}, {age: 50}})


3.查询内部嵌套文档

1. 查询tag中title为clean的document
Document.find({'tag.title': 'clean'})

// 注意:这种方式只能查询内部嵌套定义的schme,外部ref的无法使用此方法
// 外部ref:slot表,处理方法:先获得符合条件的slotIds, 再使用Document.find({slot:$in:slotIds})
  1. 查询数组:
模型:Document:{tags:['red','black']}

1. 查询tags中只有red字段的Document:['red']
Document.find({ tags: ['red']})

2. 查询tags中只有['red','black']字段的Document:['red','black'],数据库中次序不对也无法被查到
Document.find({ tags: ['red','black']})

3. 查询tags中包含有red字段的Document:['red','**']
Document.find({ tags: 'red'})

4. 查询tags中包含有'red','black'字段的Document:['red','black','clean']
Document.find({ tags: {$all: ['red', 'blank']}})

5. 查询tags第一位为'black'的Document:['red','black','clean']
Document.find({ tags: 'secondaryTags.1': 'black'})

6. 查询tags长度为2的Document
Document.find({ tags: { $size: 2 })
相关标签: Find