MongoDB:如何通过嵌套文档中的ID查找文档 [英] MongoDB: How to find a document by an id inside a nested document

查看:187
本文介绍了MongoDB:如何通过嵌套文档中的ID查找文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出这样的集合:..

Given a collection like this:..

[
  {
    "_id" : ObjectId("5546329a470000850084a621"),
    "name": "Joe",
    "surname": "Smith",
    "accounts": [
      {
        "_id" : ObjectId("5546329a470000850084a655"),
        "default": true,
        "status" : "approved",
        "activationTime" : ISODate("2013-05-03T14:37:15.025Z")
      },
      {
        "_id" : ObjectId("5546329a470000850084a688"),
        "default": true,
        "status" : "approved",
        "activationTime" : ISODate("2014-06-03T14:37:15.025Z")
      }
    ]
  },
  {
    "_id" : ObjectId("9546329a470079850084a622"),
    "name": "Jimmy",
    "surname": "Brown",
    "accounts": [
      {
        "_id" : ObjectId("5546329a470790850084a651"),
        "default": true,
        "status" : "suspended",
        "activationTime" : ISODate("2015-02-03T14:37:15.025Z")
      },
      {
        "_id" : ObjectId("5546329a470019850084a611"),
        "default": true,
        "status" : "approved",
        "activationTime" : ISODate("2015-04-03T14:37:15.025Z")
      }
    ]
  },
]

...如何通过accounts.N._id查找文档?我已经尝试过了...

... how do I find a document by accounts.N._id? I've tried this...

db.users.find(
  {},
  {
    "accounts": 0, "accounts": {
      "$elemMatch": { "_id" : ObjectId("5546329a470019850084a611"), "default": true }
    }
  }
)

...但是它不起作用,因为我只得到所有文档的_id:

... but it does't work since I get only the _id of all the documents:

{ "_id" : ObjectId("5546329a470000850084a621") }
{ "_id" : ObjectId("9546329a470079850084a622") }

我想念什么吗?

编辑

我实际需要的结果是这样的:

The result that I actually need is something like this:

{
  "_id" : ObjectId("9546329a470079850084a622"),
  "name": "Jimmy",
  "surname": "Brown"
}

例如,我需要按accounts.N._id查找,但不显示嵌套文档本身.

For instance, I need to find by accounts.N._id but without showing the nested document itself.

推荐答案

使用

当字段包含嵌入式文档时,查询可以指定 嵌入文档上的完全匹配项或通过以下方式指定匹配项 嵌入式文档中的各个字段均使用点号表示.

When the field holds an embedded document, a query can either specify an exact match on the embedded document or specify a match by individual fields in the embedded document using the dot notation.

db.coll.find({
   "accounts._id" :ObjectId("5546329a470019850084a611")
})

如果仅需要输出具有_id的数组部分,则需要使用

If you need to output only the part of an array where you have your _id you need to use dollar in projection

位置$运算符限制来自的内容 查询结果仅包含与查询匹配的第一个元素 文档.

The positional $ operator limits the contents of an from the query results to contain only the first element matching the query document.

,您的查询如下所示:

db.coll.find({
   "accounts._id" :ObjectId("5546329a470019850084a611")
}, {
   "accounts.$.": 1
})

建议.如果您需要修改后的问题中的输出,请使用以下命令:

P.S. if you need the output like in your modified questions, use this:

db.coll.find({
   "accounts._id" :ObjectId("5546329a470019850084a611")
 }, {
   accounts : 0
 })

这篇关于MongoDB:如何通过嵌套文档中的ID查找文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆