For MongoDB 4.4.6
For a collection with an array - If I use
db.fighters.find({"close_attacks": {$size: 3}},
{"_id": 0, "biography": 0})
.pretty()
db.fighters.find({"close_attacks.2" : { $exists: true }},
{"_id": 0, "biography": 0})
.pretty()
db.fighters.find({"close_attacks.3" : { $exists: true }},
{"_id": 0, "biography": 0})
.pretty()
All work in peace
I want work with >=#
(i.e >=3), I did do a research, and in many posts in SO appear two options, one already shown above: working with the index of the array and #exists, the other is working with $where
So in Compass through _mongosh beta I tried:
db.fighters.find({$where: "this.close_attacks.length >= 3"},
{"_id": 0, "biography": 0})
.pretty()
and arises
MongoError: TypeError: this.close_attacks is undefined :
@:1:15
If I remove this
db.fighters.find({$where: "close_attacks.length >= 3"},
{"_id": 0, "biography": 0})
.pretty()
it arises
MongoError: ReferenceError: close_attacks is not defined :
@:1:15
What is missing or wrong?