Is it possible to populate objects in map schema type? Is it possible to populate objects in map schema type? mongoose mongoose

Is it possible to populate objects in map schema type?


This functionality was added in Mongoose 5.10.3 (September 2020). You simply denote every element in the map with a $*.

In your example this would be:

  const request = Element.find(query, projection);  request.populate('parameters.$*.definition');  request.exec( (err, docs) => {


I also trying to find answer on this question. It seems that deep-populate work, but only if you put keys from the Map to populate method/function. In your case, if you have data like:

    {        model: ObjectId("111"),        name:       "MyName",        objectid:   111,        externalId: "ExternalId",        properties: ...,        parameters:{            "parameter1":{                "definition":ObjectId("333"),                "value":"value of parameter 1"        },            "parameter2":{                "definition": ObjectId("444"),                "value": "value of parameter 2"            }        }    }

Then you may find and populate like this:

Element.find({}).populate("parameters.parameter1.definiton")

But it's not a good solution. It would be nice if we have something like regexp inside this populate path.

Currently I've managed only to grab all inner collection, and then manually work with Map to substitude collections.It shouldn't be a huge overhead, since you have only 2 queries to DB. In you case it can be like:

    const elements = Element.find({});    const parameters = Parameter.find({});    // go through the elements.parameters and replace it with appropriate value from parameters collection.