仅当该字段存在于该文档中时才需要过滤该字段上的mongo文档

时间:2019-01-17 04:08:08

标签: mongodb filter exists

我有一个文档集合,每个文档都有自己的一组列。在所有文档中都会出现几栏,而不会出现。需要建立一个查询,仅在该列存在时才在该列中搜索文本

示例:在收藏集文章中:         db.articles.find({}, {Title:1,Conclusions:1})

    { "_id" : ObjectId("5c392c930e7c0058e781a2c4"), "Title" : "Title for article1", "Conclusions" : { "Conclusions" : "Some conclusion" } }
    { "_id" : ObjectId("5c392c930e7c0058e781a2c9"), "Title" : "Title for article2" }
    { "_id" : ObjectId("5c392c930e7c0058e781a2ce"), "Title" : "Title for article3" }
    { "_id" : ObjectId("5c392c940e7c0058e781a2d8"), "Title" : "Title for article4", "Conclusions" : { "Conclusions" : "Some other conclusion" } }

我能够检查列是否存在以下内容:

    db.articles.find({ "Conclusions": { $exists: true, $ne:null } }, {Title:1,Conclusions:1})

但无法使用它来添加第二个条件(下面什么都不返回)

    db.articles.find({ "Conclusions": { $exists: true, $ne:null }, Conclusions: '/other/i' }, {Title:1,Conclusions:1})

预期结果:

    { "_id" : ObjectId("5c392c940e7c0058e781a2d8"), "Title" : "Title for article4", "Conclusions" : { "Conclusions" : "Some other conclusion" } }

0 个答案:

没有答案