查询一个字段中的多个文本值,MongoDB
Query for more than one text value in a field, MongoDB
对于包含以下字段的文档:
{"_id" : 1, "name": "Sarah", "fruitEaten" : ["apple", "banana", "orange"]}
{"_id" : 2, "name": "Steve", "fruit Eaten" : "apple"}
{"_id" : 3, "name": "Stacey", "fruitEaten" : ["apple", "orange"]}
如何查询谁吃了一个以上的水果?
我一直在努力db.collection.find({"fruitEaten":{"$gt":1},{"name":1}})
db.collection.aggregate([
{
$match: {
$expr: {
$and: [
{
$isArray: "$fruitEaten"
},
{
$gt: [ { $size: "$fruitEaten" }, 1 ]
}
]
}
}
}
])
对于包含以下字段的文档:
{"_id" : 1, "name": "Sarah", "fruitEaten" : ["apple", "banana", "orange"]}
{"_id" : 2, "name": "Steve", "fruit Eaten" : "apple"}
{"_id" : 3, "name": "Stacey", "fruitEaten" : ["apple", "orange"]}
如何查询谁吃了一个以上的水果?
我一直在努力db.collection.find({"fruitEaten":{"$gt":1},{"name":1}})
db.collection.aggregate([
{
$match: {
$expr: {
$and: [
{
$isArray: "$fruitEaten"
},
{
$gt: [ { $size: "$fruitEaten" }, 1 ]
}
]
}
}
}
])