【问题标题】:getting error while executing mongo command执行 mongo 命令时出错
【发布时间】:2018-05-12 12:07:03
【问题描述】:

我使用以下命令创建了一个集合:

db.device_states.documentKey.insert({"device":"nest_kitchen_thermo"})

当我尝试执行以下命令时出现错误:

db.device_states.watch( {
     $match: {
             "documentKey.device": {
                   $in : [ "nest_kitchen_thermo"]
             },
             operationType: "insert"
     }
});

语法错误:缺少:属性 ID 后:

更新后的集合如下所示:

{ 
    _id: <resume_token>,
    operationType: 'insert',
    ns: {db:'example',coll:"device_states"},
    documentKey: { device:'nest_kitchen_thermo'},
    fullDocument: { 
       _id : ObjectId(),
       device: 'nest_kitchen_thermo',
       temp: 68
    }
}

【问题讨论】:

  • 你有什么问题@niroopsainni?你想达到什么目标?
  • 我的问题是 db.device_states.watch({...}) 出现错误我在创建集合时出错了,请帮助我创建集合以使用我的 db.device_states.watch({ ..)} 正确查询。
  • 我对猫鼬一无所知,我只是指出您的帖子完全不可读。如果您想引起注意,至少可以让您的问题清晰易懂。

标签: json mongodb mongoose mongodb-query mongoid


【解决方案1】:

在Mongo shell中,没有watch这样的查询属性。 $match 是聚合管道的一部分,应该在聚合操作中使用。

修改后的查询(Mongo Shell)是

db.device_states.documentKey.aggregate([
  { $match:
    { 
      "device": { $in: ["nest_kitchen_thermo"] }, 
      operationType: "insert"
    }
  }
]);

【讨论】: