【问题标题】:MongoDB, how to retrieve elements which has not a matchable element into an Array?MongoDB,如何将没有可匹配元素的元素检索到数组中?
【发布时间】:2016-06-29 21:34:07
【问题描述】:

我有这种元素:

{  "_id" : 1,
  "docs" : [
    { "key": "blah", "count": 2},
    { "key": "wow", "count": 10}
  ]
},
{  "_id" : 2,
  "docs" : [
    { "key": "blah", "count": 11}
}

我想检索未定义 docs.key == "wow" 的元素。在这种情况下,元素"_id": 2

这个查询我得到相反的结果:

db.getCollection('myCollection').find(
  {
    "docs.key": "wow"
  }
);

我尝试了$existsaggregate 的组合,但没有找到合适的解决方案。

【问题讨论】:

标签: mongodb


【解决方案1】:

你试过$ne操作符

db.getCollection('myCollection').find(
  {
    "docs.key": {$ne : "wow"}
  }
);

【讨论】:

  • Error: count failed: { "ok" : 0, "errmsg" : "$not needs a regex or a document", "code" : 2 }
【解决方案2】:

你可以这样做:

db.getCollection('myCollection').find({
    "docs.key": {$ne: "wow"}
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多