Home > Software design >  MongoDB aggregate data using $agg
MongoDB aggregate data using $agg

Time:10-26

So i try to aggregate the data from mongoDB using $match and $group to get the exact value an d format with data from mongoDB in JSON as Below:

[ {
    _id: new ObjectId("6357b4237cf79bba3e66c096"),
    userId: new ObjectId("635762fe85b94eac7466d965"),
    formId: new ObjectId("6357921d49de88bb7fffcfe4"),
    title: 'Quality',
    username: '[email protected]',
    date: '2022-10-25',
    answer: {
      '6357921d49de88bb7fffcfe8': '[email protected]',
      Text: '[email protected]',
      '6357b331235053a9d4e8d037': '[email protected]',
      Email: '[email protected]',
      '6357b335235053a9d4e8d03a': 'Maja',
      Plant: 'Maja'
    },
    createdAt: 1666692131,
    updatedAt: 1666692131,
    __v: 0
  },
  {
    _id: new ObjectId("6357b54922866fae378af370"),
    userId: new ObjectId("635762fe85b94eac7466d965"),
    formId: new ObjectId("6357921d49de88bb7fffcfe4"),
    title: 'Quality',
    username: '[email protected]',
    date: '2022-10-25',
    answer: {
      '6357921d49de88bb7fffcfe8': '[email protected]',
      Text: '[email protected]',
      '6357b331235053a9d4e8d037': '[email protected]',
      Email: '[email protected]',
      '6357b335235053a9d4e8d03a': 'Cica',
      Plant: 'Cica'
    },
    createdAt: 1666692425,
    updatedAt: 1666692425,
    __v: 0
  },
  {
    _id: new ObjectId("6357b6a2a13ab3c4a462be8f"),
    userId: new ObjectId("635647c1e24d0a4482e3c0a9"),
    formId: new ObjectId("6357921d49de88bb7fffcfe4"),
    title: 'Quality',
    username: '[email protected]',
    date: '2022-10-25',
    answer: {
      '6357921d49de88bb7fffcfe8': '12',
      Text: '12',
      '6357b331235053a9d4e8d037': '[email protected]',
      Email: '[email protected]',
      '6357b335235053a9d4e8d03a': 'Ranca',
      Plant: 'Ranca'
    },
    createdAt: 1666692770,
    updatedAt: 1666692770,
    __v: 0
  }]

using this line of code in javascript:

forms = await Answer.aggregate([
          {
            $match: { title: "Quality" },
          },
          { $group: { title: "Quality"} },

        ]);

what i expected is the result like this:

{
    formId: new ObjectId("6357921d49de88bb7fffcfe4"),
    title: 'Quality',
    username: [{'[email protected]',[email protected]}]
    date: [{'2022-10-25','2022-10-26}]
    answer: [{
      '6357921d49de88bb7fffcfe8': '[email protected]',
      Text: '[email protected]',
      '6357b331235053a9d4e8d037': '[email protected]',
      Email: '[email protected]',
      '6357b335235053a9d4e8d03a': 'Maja',
      Plant: 'Maja'
    },{
      '6357921d49de88bb7fffcfe8': '[email protected]',
      Text: '[email protected]',
      '6357b331235053a9d4e8d037': '[email protected]',
      Email: '[email protected]',
      '6357b335235053a9d4e8d03a': 'Cica',
      Plant: 'Cica'
    },...etc],
  },

i try to use more complex query but it seems it not return the result i want and i've try to seek any related problem but it wont return the same result as i want

CodePudding user response:

You have the right idea using $group, you just need to leverage it's syntax properly, like so:

db.collection.aggregate([
  {
    $match: {
      title: "Quality"
    }
  },
  {
    $group: {
      _id: "$title",
      title: {
        $first: "$title"
      },
      username: {
        "$addToSet": "$username"
      },
      date: {
        $addToSet: "$date"
      },
      formId: {
        $addToSet: "$formId"
      },
      answer: {
        $push: "$answer"
      }
    }
  }
])

Mongo Playground

  • Related