I have my sample userSchema here:
{
"_id": objectId("6092076ba811e50b565497ec"),
"username": "[email protected]",
"address_book": [{
"_id": objectId("6092b1120f7e370b954a2708"),
"address": "address1",
"address2": "address2",
}, {
"_id": objectId("6093edcb88796b0a5eba19a3"),
"address": "test1",
"address2": "test2",
}]
}
Can I find user by
objectId("6092076ba811e50b565497ec") and address_book._id object("6093edcb88796b0a5eba19a3")
and it return only the address_book that I selected? my expected return data should look like this
{
"_id": objectId("6092076ba811e50b565497ec"),
"username": "[email protected]",
"address_book": {
"_id": objectId("6093edcb88796b0a5eba19a3"),
"address": "test1",
"address2": "test2",
}
}
here is my sample function
let user = [];
await User.findOne({
_id: id,
"address_book._id": address_id,
})
.then((result) => {
console.log(result);
user = result;
})
.catch((err) => console.log(err));
return user;
with this I get all address_book
and also can is there and updateOrCreate function by address_book._id?
Thank you in advance.