Reputation: 885
When I have one item left in an array and I remove it, the field becomes empty. Is there any option to check if there is one element left and then update the key with undefined
instead of removing?
Here is what I have:
User.aggregate([
{$match: {email: {$in: [email, friendEmail]}}},
{
$project: {
friendSentRequests: {$size: {"$ifNull": ["$friendSentRequests", []]}},
friendReceivedRequests: {$size: {"$ifNull": ["$friendReceivedRequests", []]}},
}
}],
(err, result) => { ... }
);
With method written above I indeed get the array size, but batch
throws errors that array is type missing
if one doc doesn't have the key that second doc has. Unless I'm not supposed to make any updates in (err, result) => {..}
function and there is another recommended way to update the object.
I don't want to set multi
because I want to update via batch
two different keys from different docs and I can't get the array length of each of these fields. Unless I'm misunderstanding how multi
works.
Upvotes: 0
Views: 298
Reputation: 601
if you want to confirm the number of elements in an array , e.g here is you want to check that the array has one element then:
{
$match:{
"myArray" :{ $size : 1 }
}
}
And to check if array does not exist:
{
$match:{
"myArray" :{ $exists: false}
}
}
Upvotes: 2