【问题标题】:Why mongo does not return all the fields?为什么 mongo 不返回所有字段?
【发布时间】:2017-07-07 22:05:24
【问题描述】:

我有一个包含位置和名称作为字段的集合。

我用下面的猫鼬为名字创建了索引,

eventSchema.index({name: 'text'});

当我在 robomongo 上运行它时,它会返回所有 12 个字段,

db.getCollection('_event').find({"location.country":"United States"})

但是当我在 robomongo 上运行它时,它只返回两个字段的值,id 和 location,

db.getCollection('_event').find({$text: {$search: "2017 Honda"}},{"location.country":"United States"})

【问题讨论】:

    标签: node.js mongodb robo3t


    【解决方案1】:

    这是因为您放错了其他查询表达式,您将其指定为投影,因此您得到的投影包含两个字段:

    db.getCollection('_event').find(
        {$text: {$search: "2017 Honda"}}, // <-- query part
        {"location.country":"United States"}  // <-- projection part
    )
    

    您需要将其移动到查询对象中:

    db.getCollection("_event").find(
        {
            "$text": { "$search": "2017 Honda" }, 
            "location.country": "United States"
        }  
    )
    

    这是一个隐含的$and表达式,也可以显式指定为

    db.getCollection("_event").find(
        {
            "$and": [
                { "$text": { "$search": "2017 Honda" } }, 
                { "location.country": "United States" } 
            ]
        }           
    )
    

    【讨论】:

      【解决方案2】:
      db.getCollection('_event').find({
          $text: {$search: "2017 Honda"},
          "location.country":"United States"
      },
      {
          "location":1,
          "_id":0 // -- here we specify required fields. 1 is showing, 0 is not showing
      });
      

      【讨论】:

        猜你喜欢
        • 2020-05-21
        • 1970-01-01
        • 2016-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-14
        相关资源
        最近更新 更多