0

I have an API that is connected to a Mongo Database remotely hosted on EC2. I have three collection on there namely surveys, responses and questions. There "schema" is as follows:

Surveys:

{
  "_id": {
    "$oid": "62ff2e597ac958667ff8468e"
  },
  "name": "Interact 3",
  "questions": [
    {
      "$oid": "62ff2dfba14c689bde49f7d2"
    },
    {
      "$oid": "62ff2e0acb3e975ef145dad9"
    },
    {
      "$oid": "62ff2e1f3ad9cd3c6e3cb3c0"
    },
    {
      "$oid": "62ff2e311d96af83f419225f"
    }
  ],
  "responses": [],
  "description": "",
  "pic": "",
  "createdOn": {
    "$date": {
      "$numberLong": "1660890713397"
    }
  },
  "__v": 0
}

Questions:

{
  "_id": {
    "$oid": "62ff4a40c4a0bff5fb242ea8"
  },
  "hasShowPattern": false,
  "showIf": null,
  "options": [],
  "questionText": "Q1",
  "inputType": {
    "$oid": "624558d1a263f17689cdc5bc"
  },
  "createdOn": {
    "$date": {
      "$numberLong": "1660897908862"
    }
  },
  "__v": 0
}

Repsonses:

{
  "_id": {
    "$oid": "62ffcc6c5f309cb4b5ef03ec"
  },
  "surveyId": {
    "$oid": "62ff42e8f58a9fcf1eebe506"
  },
  "name": "sampke",
  "answers": [
    {
      "_id": {
        "$oid": "62ffcc5c5f309cb4b5ef03e8"
      },
      "inputType": {
        "$oid": "624558d1a263f17689cdc5bc"
      },
      "questionId": {
        "$oid": "62ff42c432a7b873a25ad462"
      },
      "answer": "Answer 2"
    },
    {
      "_id": {
        "$oid": "62ffcc605f309cb4b5ef03e9"
      },
      "inputType": {
        "$oid": "624558d1a263f17689cdc5bc"
      },
      "questionId": {
        "$oid": "62ff42cb4ad37a6b95192753"
      },
      "answer": "Answer 3"
    },
    {
      "_id": {
        "$oid": "62ffcc645f309cb4b5ef03ea"
      },
      "inputType": {
        "$oid": "624558d1a263f17689cdc5bc"
      },
      "questionId": {
        "$oid": "62ff42b465f850cecf89928e"
      },
      "answer": "Answer 1"
    },
    {
      "_id": {
        "$oid": "62ffcc685f309cb4b5ef03eb"
      },
      "inputType": {
        "$oid": "624558d1a263f17689cdc5bc"
      },
      "questionId": {
        "$oid": "62ff42d31c8d85e631515c6e"
      },
      "answer": "Answer 4"
    }
  ],
  "sentDate": {
    "$date": {
      "$numberLong": "1660941980653"
    }
  },
  "enumratorId": {
    "$oid": "62e76d2154c075f831ab9a8d"
  },
  "createdOn": {
    "$date": {
      "$numberLong": "1660931187317"
    }
  },
  "__v": 0
}

Then I wanted to join these collection in an aggregate function like this in my node code

var _survey = await Survey.aggregate([
      {
        "$match": {
          "_id": new ObjectId(surveyId)
        }
      },
      {
        "$lookup": {
          "from": "questions",
          "localField": "questions",
          "foreignField": "_id",
          "as": "joined_questions",
        }
      },
      {
        "$lookup": {
          "from": "responses",
          "localField": "responses",
          "foreignField": "_id",
          "as": "joined_responses",
        }
      }
    ]);

The above code return the questions in the new array (joined_questions) in a random order. But when i try to test is locally on my machine it works perfectly. What is causing this behavior? Does latency affect how the above code retrieves objects?

P.S I tried making an aggregation pipline on mongodb compass, it was the same result. Works on the local db, out of order on the deployed db.

Edit: The mongo version on the server is 6.0 my local one is 4.2, idt that makes a difference.

Thanks.

Joseph
  • 63
  • 1
  • 10
  • If you sort using createdOn field and apply pagination, your result would be consistent. – Gibbs Aug 19 '22 at 15:44
  • @Gibbs I just did that, the question are in order but now are out of sync with joined_responses array. – Joseph Aug 19 '22 at 17:59

0 Answers0