'Query for more than one text value in a field, MongoDB
For a document with fields:
{"_id" : 1, "name": "Sarah", "fruitEaten" : ["apple", "banana", "orange"]}
{"_id" : 2, "name": "Steve", "fruit Eaten" : "apple"}
{"_id" : 3, "name": "Stacey", "fruitEaten" : ["apple", "orange"]}
How can I query to get who ate more than one fruit eaten?
I have been trying db.collection.find({"fruitEaten":{"$gt":1},{"name":1}})
Solution 1:[1]
db.collection.aggregate([
{
$match: {
$expr: {
$and: [
{
$isArray: "$fruitEaten"
},
{
$gt: [ { $size: "$fruitEaten" }, 1 ]
}
]
}
}
}
])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |