MongoDb aggregate is reading same value multiple times

我们两清 提交于 2020-07-22 04:28:04

问题


I have this mongodb aggregate that is working and i am getting data in excel file. But in excel file i am seeing duplicate records even though it appears in the system for only one time. I am trying to understand why it is reading a value multiple times even though the filter value is occuring only once and not repeating itself. And how can i fix this issue? I know i need to make use of $group but i am having issue with its syntax so you help will be mich appreciated. Here is my aggregate

db.ParcelStatus.aggregate[
  { '$match': { createdAt: [Object], statusRepositoryId: [Object] } },
  {
    '$lookup': {
      from: 'Parcel',
      localField: 'parcelId',
      foreignField: '_id',
      as: 'parcel'
    }
  },
  { '$unwind': { path: '$parcel' } },
  {
    '$lookup': {
      from: 'ParcelStatus',
      localField: 'parcel._id',
      foreignField: 'parcelId',
      as: 'parcelStatuses'
    }
  },
  {
    '$lookup': {
      from: 'FinancialLedger',
      localField: 'parcel._id',
      foreignField: 'parcelId',
      as: 'financials'
    }
  },
  {
    '$unwind': { path: '$financials', preserveNullAndEmptyArrays: true }
  },
  {
    '$lookup': {
      from: 'CustomerData',
      localField: 'parcel.customerDataId',
      foreignField: '_id',
      as: 'customerData'
    }
  },
  { '$unwind': '$customerData' },
  { '$match': { 'customerData.cityId': [Object] } },
  {
    '$lookup': {
      from: 'Customer',
      localField: 'customerData.customerId',
      foreignField: '_id',
      as: 'customerData.customer'
    }
  },
  { '$unwind': '$customerData.customer' }
]

The issue is occuring because of statusRepositoryId in match filter as that id is the one i want to get grouped

here is a duplicate record in excel file

Sample JSON structre for ParcelStatus

[
  {
    "id": "string",
    "parcelId": "string",
    "vendorId": "string",
    "createdAt": "2020-07-17T07:23:17.630Z",
    "updatedAt": "2020-07-17T07:23:17.630Z",
    "statusRepositoryId": "string"
  }
]

Sample JSON structure for financialLedger

[
  {
    "vendorId": "string",
    "parcelId": "string",
    "mutatedParcel": {},
    "id": "string",
    "createdAt": "2020-07-17T07:23:15.906Z",
    "updatedAt": "2020-07-17T07:23:15.906Z"
  }
]

Sample Parcel JSON

{
  "id": "RWP597972414215",
  "currentStatusId": "5dd7fa20dcfa9600152cc2d8",
  "zoneAreaId": "ZONE_NOT_DEFINED",
  "vendorId": "5de137b8f0230b00128f9ae4",
  "createdAt": "2020-07-16T08:23:30.028Z",
  "updatedAt": "2020-07-16T08:54:12.441Z",
  "pickupLocationId": "PL-831075",
  "description": "asdasdf",
  "customerDataId": "5f100e816811b9a11c3d5487",
  "previousStatusId": "5ee134230138634c27a6e1da",
  "loadsheetId": "LS-398866",
}

Sample Data JSON

{
    _id: 5efd93b80674f64f609f054d,
    parcelId: 'LHE044832446160',
    vendorId: 5e074ad0b0daa00012a4ebd0,
    createdAt: 2020-07-02T07:58:48.859Z,
    updatedAt: 2020-07-02T07:58:48.859Z,
    statusRepositoryId: 5dd7fa20dcfa9600152cc2e2,
    parcel: {
      _id: 'LHE044832446160',
      pickupLocationId: 'PL-379331',
      customerDataId: 5ef32b5f2141d84348b74770,
      currentStatusId: 5dd7fa20dcfa9600152cc2e2,
      previousStatusId: 5dd7fa20dcfa9600152cc2e3,
      loadsheetId: 'LS-834583',
      vendorId: '5e074ad0b0daa00012a4ebd0'
    },
    parcelStatuses: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object]
    ],
    financials: {
      _id: 5ef9b9989623f92d6c88b6c1,
      vendorId: 5e074ad0b0daa00012a4ebd0,
      parcelId: 'LHE044832446160',
      mutatedParcel: [Object],
      createdAt: 2020-06-29T09:51:20.303Z,
      updatedAt: 2020-06-29T09:51:20.303Z
    },
    customerData: {
      _id: 5ef32b5f2141d84348b74770,
      firstName: 'Ali Murtaza Hussain',
      email: 'test@test.com',
      lastName: 'Ali Murtaza Hussain',
      vendorId: 5e074ad0b0daa00012a4ebd0,
      createdAt: 2020-06-24T10:30:55.618Z,
      updatedAt: 2020-06-29T09:51:37.736Z,
      customerId: 5ef32b5f2141d84348b7476f,
      customer: [Object]
    }
  },

I have changed Ids and removed some other confidential data

来源:https://stackoverflow.com/questions/62941489/mongodb-aggregate-is-reading-same-value-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!