0

something is wrong with my aggregation function. I get duplicate elements.

I try it with this: MongoDB sort documents by array elements

console.log('####');
console.log('offset', offset);
console.log('pageSize', pageSize);
ProcedureModel.aggregate([
    { $match: { 'history.initiator': '2. Beratung' } },
    {
      $addFields: {
        order: {
          $arrayElemAt: [
            {
              $filter: {
                input: '$history',
                as: 'p',
                cond: { $eq: ['$$p.initiator', '2. Beratung'] },
              },
            },
            0,
          ],
        },
      },
    },
    { $sort: { 'order.date': -1 } },
    { $skip: offset },
    { $limit: pageSize },
  ]).then((res) => {
    res.forEach((r, i) => console.log(`${i}: `, r._id));
    return res;
  });

My objects looks like this

{
"_id" : ObjectId("5a75a91ea57539646d00be29"),
"procedureId" : "81080",
"history" : [ 
    {
        "_id" : ObjectId("5a75a91d4a5aa5541db67598"),
        "initiator" : "Änderung der Ausschussüberweisung",
        "date" : ISODate("2017-06-01T00:00:00.000Z"),
    }, 
    {
        "_id" : ObjectId("5a75a91d4a5aa5541db67597"),
        "initiator" : "Beschlussempfehlung und Bericht,  Urheber : Ausschuss für Recht und Verbraucherschutz",
        "date" : ISODate("2017-06-28T00:00:00.000Z")
    }, 
    {
        "_id" : ObjectId("5a75a91d4a5aa5541db67596"),
        "initiator" : "2. Beratung",
        "date" : ISODate("2017-06-30T00:00:00.000Z"),
        …
    }, 
    …
],
…

}

and this is my result. there are two duplicate results :(

####
offset 0
pageSize 3
0:  5a75a91ea57539646d00be29
1:  5a75a91ea57539646d00bdb6
2:  5a75a91ea57539646d00bab7
####
offset 3
pageSize 3
0:  5a75a91ea57539646d00be65
1:  5a75a91ea57539646d00be29
2:  5a75a91ea57539646d00bab7
####
offset 6
pageSize 3
0:  5a75a91ea57539646d00b9eb
1:  5a75a91ea57539646d00bba5
2:  5a75a91ea57539646d00bb9f

why did have the second request two results of the first request, and the third none of the previous? and how I set up a correct offset and limit?

2
  • Kindly paste the documents that are returned with the query ProcedureModel.aggregate([ { $match: { 'history.initiator': '2. Beratung' } }]).exec((err, docs) => console.log(docs)); Commented Feb 3, 2018 at 13:53
  • the result without skip and limit is correct: 0: 5a75c209a57539646d00c6a4 1: 5a75c209a57539646d00c6a6 2: 5a75c209a57539646d00c6ae 3: 5a75c209a57539646d00c6b6 4: 5a75c66ca57539646d00cb7f 5: 5a75c66ca57539646d00ce26 6: 5a75c66ca57539646d00ced7 7: 5a75c66ca57539646d00cee5 8: 5a75c66ca57539646d00cef2 9: 5a75c66ca57539646d00cf34 10: 5a75c66ca57539646d00cac2 (i reimport my data, so the id's are different) Commented Feb 3, 2018 at 17:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.