mongoose MongoDB,返回带有聚合操作的文档中的选定字段

9bfwbjaz  于 2023-01-09  发布在  Go
关注(0)|答案(1)|浏览(108)

对于给定的查询,我还希望返回productId。
我有一个集合 comments,其中包含带有给定产品的productId和注解相关数据的文档
此集合中的示例文档:

{
  "_id": {
    "$oid": "635ee64f55460d1796447662"
  },
  "productId": "63413800d36ed477adc763d0",
  "__v": 0,
  "comments": [
    {
      "userId": "",
      "userName": "test",
      "date": "2022.12.18.21.51.36",
      "confirmed": false,
      "likes": {
        "up": 0,
        "down": 0
      },
      "content": {
        "rating": 6,
        "description": "testtesttest"
      },
      "image": {
        "added": false,
        "images": []
      },
      "_id": {
        "$oid": "639f7d58b6206a863c4a7aba"
      },
      "usersWhoLiked": []
    },
    {
      "userId": "",
      "userName": "test",
      "date": "2022.12.18.21.52.19",
      "confirmed": false,
      "likes": {
        "up": 0,
        "down": 0
      },
      "content": {
        "rating": 6,
        "description": "testtesttest"
      },
      "image": {
        "added": true,
        "images": [
          "comments/63413800d36ed477adc763d0/639f7d83b6206a863c4a7ad6/dell.jpg"
        ]
      },
      "_id": {
        "$oid": "639f7d83b6206a863c4a7ad6"
      },
      "usersWhoLiked": []
    }
  ]
}

我的示例查询:

db.comments.aggregate([{$match: {"comments._id": {$in: [ObjectId('630b7868f51e10876223b4aa'), ObjectId('630bd277f919a9e9c0e7a559')]}}},
{$project: {comment: {$filter: {input: "$comments", as: "comment", cond: {$in: ["$$comment._id", [ObjectId("630b7868f51e10876223b4aa"), ObjectId("630bd277f919a9e9c0e7a559")]]}}}}}])

使用此查询,我得到的结果是:

{ _id: ObjectId("630b7868f51e10876223b4a6"),
  comment: 
   [ { userId: '62f29c2c324f4778dff443f6',
       userName: 'User',
       date: '2022.08.19',
       confirmed: false,
       likes: { up: 3, down: 0 },
       content: { rating: 4, description: 'Super laptop <3' },
       _id: ObjectId("630b7868f51e10876223b4aa"),
       usersWhoLiked: 
        [ { userId: '62f29c2c324f4778dff443f6',
            likeUp: true,
            _id: ObjectId("630d2b0494370efb37107983") },
          { userId: '6322434f2b5bbac87f0e7aba',
            likeUp: true,
            _id: ObjectId("632243702b5bbac87f0e7afa") },
          { userId: '62f2991e324f4778dff443d4',
            likeUp: true,
            _id: ObjectId("63af4d77c8991b74d6986995") } ] } ] }
{ _id: ObjectId("630bd277f919a9e9c0e7a555"),
  comment: 
   [ { userId: '62f29c2c324f4778dff443f6',
       userName: 'User',
       date: '2022.08.28',
       confirmed: false,
       likes: { up: 1, down: 1 },
       content: 
        { rating: 6,
          description: 'Laptop posiada przyjemna klawiature, nie grzeje się. Do codziennego grania wystarczy.' },
       _id: ObjectId("630bd277f919a9e9c0e7a559"),
       usersWhoLiked: 
        [ { userId: '62f29c2c324f4778dff443f6',
            likeUp: true,
            _id: ObjectId("630d2dfc94370efb37107991") },
          { userId: '62fa2549f029348f75bc9c81',
            likeUp: false,
            _id: ObjectId("631241fe755c641525dc9cfa") } ] } ] }

正如您在结果中看到的,缺少productId。我尝试使用#group运算符重建查询,但仍然没有效果...因此我的问题是:我应该如何重写该查询以获得相同的结果,但对于每个返回的注解,查询中都包含productId

bakd9h0s

bakd9h0s1#

这就是$project的工作原理,如果未指定字段,则不会输出。
因此,只需将productId: 1添加到$project阶段,它就会显示出来。
检查this example

相关问题