search

Home  >  Q&A  >  body text

mongodb - How does mongoose query data based on the id of a subdocument?

{
    "_id": "590ad2c78eb79d16bde4a15d",
    "user": "yejia@qq.com",
    "password": "111111",
    "__v": 0,
    "lists": [
      {
        "_id": "590ad2d78eb79d16bde4a15e",
        "update_at": "2017-05-04T07:05:52.174Z",
        "create_at": "2017-05-04T07:05:52.174Z",
        "title": "测试1",
        "isChecked": [
          false
        ],
        "details": [
          "未完"
        ]
      },
      {
        "_id": "590ad3398eb79d16bde4a15f",
        "update_at": "2017-05-04T07:07:28.784Z",
        "create_at": "2017-05-04T07:07:28.784Z",
        "title": "测试2",
        "isChecked": [
          false,
          false
        ],
        "details": [
          "1232",
          "333"
        ]
      }
    ]
  }

Like this example, if I want to find a subdocument based on its id: "_id": "590ad2d78eb79d16bde4a15e", what should I do? To explain, I know how to query directly by id. What I don’t understand is how to query subdocuments using the id of the subdocument.

为情所困为情所困2740 days ago685

reply all(2)I'll reply

  • 仅有的幸福

    仅有的幸福2017-05-17 10:05:14

    You can view the API documentation of mongoose, the chapter about sub_doc operation, http://mongoosejs.com/docs/subdocs.html

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-17 10:05:14

    yourCollectionName.lists.id(your_id)

    reply
    0
  • Cancelreply