Node.js mongodb$unwind产生多个结果

Node.js mongodb$unwind产生多个结果,node.js,mongodb,mongoose,nosql,aggregation-framework,Node.js,Mongodb,Mongoose,Nosql,Aggregation Framework,我试图在模式中释放$unwind history数组,但它会生成多个父对象, 下面是聚合片段 [{$lookup: { from: 'places', localField: 'place', foreignField: '_id', as: 'placeInfo' }}, {$unwind: { path: '$offers' }}, {$unwind: { path: '$history' }}, {$lookup: { from: 'users',

我试图在模式中释放$unwind history数组,但它会生成多个父对象, 下面是聚合片段

[{$lookup: {
  from: 'places',
  localField: 'place',
  foreignField: '_id',
  as: 'placeInfo'
}}, {$unwind: {
  path: '$offers'
}}, {$unwind: {
  path: '$history'
}}, {$lookup: {
      from: 'users',
      localField: 'user',
      foreignField: '_id',
      as: 'userInfo'
}}, {$lookup: {
  from: 'countries',
  localField: 'deliveryAddress.country',
  foreignField: '_id',
  as: 'countryInfo'
}}, {$lookup: {
  from: 'admins',
  localField: 'history.by.id',
  foreignField: '_id',
  as: 'adminInfo'
}}, {$lookup: {
  from: 'pms',
  localField: 'history.by.id',
  foreignField: '_id',
  as: 'pmInfo'
}}, {$lookup: {
      from: 'offers',
      localField: 'offers.offer',
      foreignField: '_id',
      as: 'offerInfo'
}}, {$project: {
  transactionID: 1,
  totalPrice: 1,
  createdAt: 1,
  note: 1,
  status: 1,
  offers: 1,
  payment: 1,
  deliveryAddress: 1,
  user: 1,
  offersQuantity: {
    $sum: '$offers.quantity'
  },
  productsQuantity: {
    $sum: '$products.quantity'
  },
  place: {
    name: {
      $arrayElemAt: ['$placeInfo.name.en', -1]
    },
    branch: {
      $arrayElemAt: ['$placeInfo.branchName.en', -1]
    },
    id: {
      $arrayElemAt: ['$placeInfo._id', -1]
    }
  },
  user: {
    firstName: {
      $arrayElemAt: ['$userInfo.firstName', -1]
    },
    lastName: {
      $arrayElemAt: ['$userInfo.lastName', -1]
    },
    mobile: {
      $arrayElemAt: ['$userInfo.mobile', -1]
    },
    id: {
      $arrayElemAt: ['$userInfo._id', -1]
    }
  },
  deliveryAddress: {
    country: {
      name: {
        $arrayElemAt: ['$countryInfo.name.en', -1]
      },
      id: {
        $arrayElemAt: ['$countryInfo._id', -1]
      }
    },
      city: 1,
      area: 1,
      address: 1,
      nearestLandmark: 1
  },
  offers: [{
    quantity: '$offers.quantity',
    price: '$offers.price',
    offerDetails: {
      $arrayElemAt: ['$offerInfo', -1]
    },
  }],
  history: [{
    status: '$history.status',
    createdAt: '$history.createdAt',
    by: {
      id: '$history.by.id',
      role: '$history.by.role',
      USER: {
        $arrayElemAt: ['$userInfo', -1]
      },
      ADMIN: {
        $arrayElemAt: ['$adminInfo', -1]
      },
      PM: {
        $arrayElemAt: ['$pmInfo', -1] 
      }
    }
  }]
}}, {$match: {
 _id: ObjectId('5e26e21bd1774f60d6947aab')
}}]
以及输出:

_id:5e26e21bd1774f60d6947aab
totalPrice:5000
deliveryAddress:Object
transactionID:"Q-21"
user:Object
place:Object
status:"DELIVERING"
createdAt:2020-01-21T11:35:55.386+00:00
offers:Array
offersQuantity:10
productsQuantity:0
**history**: 

status:"PENDING"
createdAt:2020-01-21T11:34:40.682+00:00
by:Object
id:5db039c8082b8b00132b7a62
role:"USER"
USER:Object


...rest of payload
history: status:"RECEIVED"
createdAt:2020-01-21T11:34:40.682+00:00
by:Object
id:5d7908e37c20a500131d133b
role:"ADMIN"
USER:Object
ADMIN:Object

...res of payload 
history: status:"CANCELLED"
createdAt:2020-01-21T15:04:27.812+00:00
by:Object
id:5d7908e37c20a500131d133b
role:"ADMIN"
USER:Object
ADMIN:Object
只有在展开历史记录时才会发生这种情况,它会生成父结果的四个对象,其中包含不同的历史记录对象,我希望结果为一个对象,排列对象的历史记录数组如下所示:

...rest of payload,
history: [{
status:"PENDING"
createdAt:2020-01-21T11:34:40.682+00:00,
...
}, {
status:"RECEIVED"
createdAt:2020-01-21T11:34:40.682+00:00,
...
}, {
status:"CANCELLED"
createdAt:2020-01-21T15:04:27.812+00:00,
...
}]


这是$unwind操作员的正常行为

如果要在$unwind操作后对对象进行分组,则应执行以下操作

{
  $group:
     {
        _id: "$offers"
        ...
     }
}


所有内容都来自官方文档

如果你能创建一个沙箱,你就有更多机会得到一个有效的答案。