【问题标题】:MongoDB find() to only return results when a certain parameter is setMongoDB find() 仅在设置某个参数时返回结果
【发布时间】:2013-11-25 12:30:02
【问题描述】:

查看文档here 我找不到任何与此查询匹配的内容。例如,假设我想查找集合products,它只返回参数maker != undefined 的结果。示例集:

[{
    name: 'Obj1',
    maker: 'Maker1'
}.{
    name: 'Obj2'
}.{
    name: 'Obj13',
    maker: 'Maker2'
}]

在上面的示例中,只应返回对象 1 和 3。

从文档中,我认为 projections 可能是我正在寻找的,但这只是返回某些字段,而不是将结果限制为某个字段是否存在。

我怎样才能做到这一点?

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    使用$exists 运算符选择包含特定字段的文档:

    db.products.find({maker: {$exists: true}})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-19
      • 1970-01-01
      相关资源
      最近更新 更多