MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

Modifying structure to prevent using lookup

Hey all,

I’ve got a question about the structure of my data, and to see if there is a more efficient way of modifying my data to prevent the use of $lookup. This is a rough structure of my data:

SystemSchema: {
 id:
 name:
 .
 .
 statics: [array of numbers]
}
StaticSchema: {
 id:
 name:
}

I am using a $lookup on the statics array, each of which map to a StaticSchema on the id field (Essentially a 1:Many relationship). Is it possible to quickly modify the statics array and replace the numeric ids with the ObjectIDs of the StaticSchemas?

Yes. Is there a relationship between the numeric ids and the the ObjectIDs (i.e., numeric id is to be replaced with a corresponding ObjectID)? Or, is it just replacing the whole array with newer one with ObjectIDs?


I’ve got a question about the structure of my data, and to see if there is a more efficient way of modifying my data to prevent the use of $lookup.

Post the $lookup query you are using now.

Sure! Here is one item in the systems collection:

{
    "_id" : ObjectId("5e84afc0cb954cbb64b4dfeb"),
    "id" : 31001421,
    ...
    "statics" : [
        30690,
        30691
    ],
}

Here is the item from the statics collection that has ID 30690

{
    "_id" : ObjectId("5e84b0d550824abdb757eacf"),
    "id" : 30690,
    "name" : "Example"
}

This is the $lookup query mongoose is essentially making:

db.systems.aggregate([
  {
    $match: {
      "id": 31001421
    }
  },
  {
    $lookup: {
      from: "statics",
      localField: "statics",
      foreignField: "id",
      as: "staticObjects"
    }
  }
]

As for the first question, I am trying to replace the numeric ID with the ObjectID

How does this serve “Modifying structure to prevent using lookup”? It only changes the fields in the lookup.