[英]mongodb - query documents by value of field name
Given I have these documents in User
collection: 鉴于我在
User
集合中有这些文档:
{
userId: "user-a",
points: {
date: {
1: 100,
5: 20,
11: 65,
},
},
}
{
userId: "user-b",
points: {
date: {
31: 20,
25: 40,
11: 15,
},
},
}
What I want to do is I want to query users with points.date.[number]
value exist and [number]
< 7 for example. 我想要做的是我想用
points.date.[number]
查询用户points.date.[number]
值存在, [number]
<7例如。 How can I do that ? 我怎样才能做到这一点 ?
Since your keys are unknown you have to convert them to some key value pair using $objectToArray
aggregation and then can easily $match
with it 由于您的密钥未知,您必须使用
$objectToArray
聚合将它们转换为某个键值对,然后可以轻松地与它$match
db.collection.aggregate([
{ "$match": { "points.date[number]": { "$exists": true }}},
{ "$addFields": {
"match": {
"$objectToArray": "$points.date"
}
}},
{ "$match": { "match.k": { "$lt": "7" }}},
{ "$project": { "match": 0 }}
])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.