MongoDB使用$agg聚合数据

ryoqjall  于 2022-11-03  发布在  Go
关注(0)|答案(1)|浏览(184)

因此,我尝试使用$match和$group聚合mongoDB中的数据,以获得mongoDB中JSON格式的数据的确切值和格式,如下所示:

[ {
    _id: new ObjectId("6357b4237cf79bba3e66c096"),
    userId: new ObjectId("635762fe85b94eac7466d965"),
    formId: new ObjectId("6357921d49de88bb7fffcfe4"),
    title: 'Quality',
    username: 'Jansenstan24@gmail.coms',
    date: '2022-10-25',
    answer: {
      '6357921d49de88bb7fffcfe8': 'john@nabatisnack.com',
      Text: 'john@nabatisnack.com',
      '6357b331235053a9d4e8d037': 'john@dose.com',
      Email: 'john@dose.com',
      '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: 'Jansenstan24@gmail.coms',
    date: '2022-10-25',
    answer: {
      '6357921d49de88bb7fffcfe8': 'john@dose.com',
      Text: 'john@dose.com',
      '6357b331235053a9d4e8d037': 'john@nabatisnack.com',
      Email: 'john@nabatisnack.com',
      '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: 'adam@wegodev.com',
    date: '2022-10-25',
    answer: {
      '6357921d49de88bb7fffcfe8': '12',
      Text: '12',
      '6357b331235053a9d4e8d037': 'john@dose.coms',
      Email: 'john@dose.coms',
      '6357b335235053a9d4e8d03a': 'Ranca',
      Plant: 'Ranca'
    },
    createdAt: 1666692770,
    updatedAt: 1666692770,
    __v: 0
  }]

在JavaScript中使用以下代码行:

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

        ]);

我所期待的结果是这样的:

{
    formId: new ObjectId("6357921d49de88bb7fffcfe4"),
    title: 'Quality',
    username: [{'Jansenstan24@gmail.coms',adam@wegodev.com}]
    date: [{'2022-10-25','2022-10-26}]
    answer: [{
      '6357921d49de88bb7fffcfe8': 'john@nabatisnack.com',
      Text: 'john@nabatisnack.com',
      '6357b331235053a9d4e8d037': 'john@dose.com',
      Email: 'john@dose.com',
      '6357b335235053a9d4e8d03a': 'Maja',
      Plant: 'Maja'
    },{
      '6357921d49de88bb7fffcfe8': 'john@dose.com',
      Text: 'john@dose.com',
      '6357b331235053a9d4e8d037': 'john@nabatisnack.com',
      Email: 'john@nabatisnack.com',
      '6357b335235053a9d4e8d03a': 'Cica',
      Plant: 'Cica'
    },...etc],
  },

我尝试使用更复杂的查询,但它似乎没有返回我想要的结果,我已经尝试寻找任何相关的问题,但它不会返回与我想要的相同的结果

ql3eal8s

ql3eal8s1#

使用$group的想法是正确的,您只需要正确地利用它的语法,如下所示:

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

相关问题