Home > database >  Sorting MongoDB result by another MongoDB result is returning [null, null, null]
Sorting MongoDB result by another MongoDB result is returning [null, null, null]

Time:09-28

I'm performing two MongoDB queries, and then I want to synchronize the resulting arrays, to make sure they are in the same order.

The first array is a set of (20) questions ids (this is the correct order):

q_id_arr: [
   "5f86da2d37e3d200040ba523",
   "5f86b6ce37e3d200040ba4c6",
   "5ffc4abea04f3c0004e46cf3",
   "5f86b66537e3d200040ba4c5",
   "5f87f368554f370004ed17b4",
   "5f86e48c37e3d200040ba53c",
   "5ffc4dc4a04f3c0004e46d0b",
   "5f86e19037e3d200040ba534",
   "5f86aaa237e3d200040ba49b",
   "5ffc479ba04f3c0004e46ce0",
   "5f86b9dc37e3d200040ba4d2",
   "5f85828e0e1bd30004361430",
   "5f8700c937e3d200040ba548",
   "5f86d81737e3d200040ba51c",
   "5f8708d237e3d200040ba568",
   "5f87060d37e3d200040ba55c",
   "5f857dac0e1bd3000436141c",
   "5f85703e0e1bd300043613ec",
   "5f87e9d4554f370004ed178e",
   "5f8073c04ad88e00041f015f"
]

The second array is a set of (20) results associated with the question ids:

team_trends: [
   {
      "_id":"5f87e9d4554f370004ed178e",
      "positive":0.93,
      "engaged":0.558
   },
   {
      "_id":"5f86e19037e3d200040ba534",
      "positive":0.585,
      "engaged":0.567
   },
   {
      "_id":"5f85828e0e1bd30004361430",
      "positive":0.7,
      "engaged":0.666
   },
   {
      "_id":"5f8073c04ad88e00041f015f",
      "positive":0.31,
      "engaged":0.30999999999999994
   },
   {
      "_id":"5f87f368554f370004ed17b4",
      "positive":0.5449999999999999,
      "engaged":0.57
   },
   {
      "_id":"5f86b6ce37e3d200040ba4c6",
      "positive":0.855,
      "engaged":0.46599999999999997
   },
   {
      "_id":"5f857dac0e1bd3000436141c",
      "positive":0.92,
      "engaged":0.524
   },
   {
      "_id":"5f85703e0e1bd300043613ec",
      "positive":0.15,
      "engaged":0.39
   },
   {
      "_id":"5f86aaa237e3d200040ba49b",
      "positive":0.15000000000000002,
      "engaged":0.584
   },
   {
      "_id":"5f86b66537e3d200040ba4c5",
      "positive":0.37,
      "engaged":0.386
   },
   {
      "_id":"5f86e48c37e3d200040ba53c",
      "positive":0.615,
      "engaged":0.548
   },
   {
      "_id":"5ffc479ba04f3c0004e46ce0",
      "positive":0.42000000000000004,
      "engaged":0.583
   },
   {
      "_id":"5f86b9dc37e3d200040ba4d2",
      "positive":0.68,
      "engaged":0.662
   },
   {
      "_id":"5f86d81737e3d200040ba51c",
      "positive":0.03,
      "engaged":0.516
   },
   {
      "_id":"5f87060d37e3d200040ba55c",
      "positive":0.14,
      "engaged":0.454
   },
   {
      "_id":"5f86da2d37e3d200040ba523",
      "positive":0.47,
      "engaged":0.41500000000000004
   },
   {
      "_id":"5f8708d237e3d200040ba568",
      "positive":0.17,
      "engaged":0.76
   },
   {
      "_id":"5ffc4dc4a04f3c0004e46d0b",
      "positive":0.395,
      "engaged":0.53
   },
   {
      "_id":"5ffc4abea04f3c0004e46cf3",
      "positive":0.365,
      "engaged":0.679
   },
   {
      "_id":"5f8700c937e3d200040ba548",
      "positive":0.93,
      "engaged":0.6980000000000001
   }
]

I want to reorganize team_trends into the same order as q_id_arr

Here is the code I'm using (following this SO Answer):

let c = [];
q_id_arr.forEach((q_oid => c.push(team_trends.find((obj => obj._id == q_oid)))));

However when I print console.log("the result of c" c) I get this result:

the result of c: [null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]

Is this the right approach? Any suggestions are appreciated!

More Details:

Before this step I acquired the q_id_arr through mapping over an aggregate result, like this:

let q_id_arr = await user_trends.map(({ question_oid }) => question_oid)

When I tested console.log(typeof q_id_arr) it returned object.

How can I sort through the object??

CodePudding user response:

You can try this using the arrays map method:

let sorted_team_trends = q_id_arr.map( q => team_trends.find(t => t._id === q) );

Assuming the two arrays are defined as fields like this:

let q_id_arr = [
        "5f86da2d37e3d200040ba523",
        "5f86b6ce37e3d200040ba4c6",
        "5ffc4abea04f3c0004e46cf3",
        ...
];

let team_trends = [
        {
                "_id" : "5f87e9d4554f370004ed178e",
                "positive" : 0.93,
                "engaged" : 0.558
        },
        {
                "_id" : "5f86e19037e3d200040ba534",
                "positive" : 0.585,
                "engaged" : 0.567
        },
        ...
]

CodePudding user response:

Final code that fixed the issue.

user_trends.forEach((user => c.push(team_trends.find((obj => obj._id.toString() === user.question_oid.toString())))));

A couple of points to note here:

  1. If you are using mongoose then it already returns an array from the aggregate function.
  2. Mongoose uses the MongoDB NodeJs native driver at its core. In core driver ObjectId has a function .equals(otherId). It is always best to use this function for id comparisons.

References:

  1. Comparing mongoose _id and strings
  2. .aggregate(...).toArray is not a function
  3. https://mongodb.github.io/node-mongodb-native/api-bson-generated/objectid.html#equals
  • Related