in this example I want to retrieve the fields name, droit and in the object devices I only want to retrieve the position and the imei. Knowing that I do not know the key of the object.This is what I tried but it doesn't work for devices.
User.aggregate([
{ "$match": {
"_id":mongoose.Types.ObjectId(userid),
}},
{"$project": {
devices:1,name:1,droit:1,'devices.data':-1,'devices.driver':1
}}
]).then( docs => {
if (docs) {
res.status(200).send(docs);return;
}
res.status(200).send([]);
}).catch(e => { console.err(e); res.send(e); })
Example document:
{
"_id": new ObjectId("616c07d90de99f6f74f911f4"),
"name": {
"prenom": "Mike",
"nom": "Peaterson"
},
"created": new Date("Fri Oct 22 2021 13:13:30 GMT+0000 (temps universel coordonné)"),
"login": "demo",
"password": "123456",
"droit": 1,
"config": {
"sosAlarm": "1"
},
"email": "mike@demo.com",
"telephone": "77433234",
"devices": {
"8673704667584": {
"driver": {
"prenom": ""
},
"imei": "867717046675865",
"note": "c'est le dernier teste",
"created": new Date("Fri Sep 16 2022 19:03:09 GMT+0000 (temps universel coordonné)"),
data:[]
}
}
}