I'm using mongoose-paginate-v2 and also i'm using populate. It works but when i populate my field, the population returns an array and i want to put the name of the value inside of the field cajaID:
What i want to get:
{
"_id": "5aa23958-06a9-4ff1-b614-d112d81b2eSi",
"nombre": "Zone",
"cajaID": "Caja grande"
}
What i get:
{
"_id": "5aa23958-06a9-4ff1-b614-d112d81b2eSi",
"nombre": "Zone",
"cajaID": {
"nombre": "Caja grande"
}
}
My code:
await ZonesModel.paginate(query, { populate: {path: 'cajaID', select: { '_id': 0,'nombre':1}}}, function(err, doc) {
if (err) {
error = new Error(err);
error.status = 500;
throw error;
}
data = doc;
})
mongoose-paginate-v2 also has projection but when i want to access to the field "nombre" it doesn't work, just returns the value of the field which has the id of the another collection.
await model.paginate(query, { populate: {path: 'cajaID', select: { '_id': 0,'nombre':1}}, projection: { caja: "$cajaID" }}, function(err, doc) {
if (err) {
error = new Error(err);
error.status = 500;
throw error;
}
data = doc;
})
This code return this:
{
"_id": "5aa23958-06a9-4ff1-b614-d112d81b2eSi",
"nombre": "Zone",
"cajaID": {
"nombre": "Caja grande"
},
"caja": "5aa23958-06a9-4ff1-b614-d112d81b2eBa" // id of the another collection but i can't access to the nombre field
}