从mongodb获取嵌套模型引用

时间:2018-02-13 13:15:37

标签: mongodb mongoose mongodb-query node-mongodb-native

  • 我在下面有代码来获取用户的书签博客

    问题 - >我还想要用户对象选择字段

    Bookmarkblog.aggregate([{
      $lookup:
         {
            from: "blogs",
            localField: "blog",
            foreignField: "_id",
            as: "blog"
        }
     }, {
      $match: { "user": mongoose.Types.ObjectId(req.headers.userid) }
     }])
    

    当前输出

    [
      {
        "_id": "5a82ce0d80f62b18bc5ac5bf",
        "blog": [
          {
            "_id": "5a696a31f78861dd29bec5bd",
            "user": "59bbbe071b4a358f43dc0cbf",
            "category": "59c3b5022ad1854d496cfb8b",
            "image": [
              "57904805-imac_pro_large.jpg"
            ],
            "tags": [
              "Mac"
            ],
            "content": "",
            "title": "iMac Pro, the most powerful Mac ever",
            "created": "2018-01-25T05:25:05.025Z",
            "__v": 0
          }
        ],
        "user": "59e5a62427543c54603050de",
        "created": "2018-02-13T11:37:49.071Z",
        "__v": 0
      }
    ]
    

0 个答案:

没有答案