Home > Software design >  MongoDB, return selected fields from document with aggregation operations
MongoDB, return selected fields from document with aggregation operations

Time:01-09

With given query I also want to return productId.

I have collection comments that contains documents with data about productId and comments for given product

Example document in this collection:

{
  "_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": []
    }
  ]
}

My exmaple query:

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")]]}}}}}])

With this query I get the result :

{ _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") } ] } ] }

As you see in the result, the productId is missing. I was trying to rebuild query with #group operator but still with out effect... So my questsion is: How shall I rewrite that query to get the same result but with productId in it for each returned comment

CodePudding user response:

This is how $project works, if a field is not specified will not be output.

So just add productId: 1 into the $project stage and it will be shown.

Check this example.

  • Related