Hello,
Let me first provide the context of what is my database and what I want to with it. So, I have a database of song names and their ranks in music chart. What I want to do is
- I will query a document with Song’s name
- Then I want to check if Length of Array is more than certain length, like 50;
if that condition is
TRUE, more than 50, I want to pop the first element from array than push new data as element to that array.
FALSE, less than 50, just push a new data as element to array. - I want to get the upsert option to be true as well incase if document has not been found. So I tried as the follows
let condition = {
$cond:{
if:{ $gte:[{$size:"Ranking"},10]},
then:{$and:[{$pop:{Ranking: -1}},{$push:{Ranking:123}}]},
else:{$push:{Ranking:345}}
}
}
db.collection('musicChart').findOneAndUpdate({Song:'Beatbox'},{$expr:{Ranking:{condition}}},{upsert:true})
But I got an error of Unknown modifier: $expr.
I tried to search forums for the answers but I can’t find the exact of what I want to do. So I decided to post here.
!