【发布时间】:2022-01-15 07:25:10
【问题描述】:
基本上,我有一个带有嵌套对象数组的对象数组。我想根据"userID"和"flashcardReversed.value" + "flashcardReversed.count"之和查询数据。
用户将提供所需的用户 ID 和(值 + 计数)值。
这是我的数据:
[
{
"_id": "608642db80a36336946620aa",
"userID": "user1",
"title": "title2",
"flashcardReversed": [
{
"_id": "608d5b290e635ece6828141X",
"front": "2front",
"back": "2back",
"value": 1,
"count": 2
},
{
"_id": "608t5b290e635ece6828141Y",
"front": "2frontReversed",
"back": "2backReversed",
"value": 2,
"count": 3
},
{
"_id": "608a5b31a3f9806de253726X",
"front": "2front2",
"back": "2back2",
"value": 3,
"count": 4
},
{
"_id": "608a5b31a3f9806de253726Y",
"front": "2frontReversed2",
"back": "2backReversed2",
"value": 4,
"count": 5
}
]
},
{
"_id": "608642db80a36336946620aa",
"userID": "user1",
"title": "title3",
"flashcardReversed": [
{
"_id": "608d5b290e635ece6828142X",
"front": "2front",
"back": "2back",
"value": 12,
"count": 6
},
{
"_id": "608t5b290e635ece6828143Y",
"front": "2frontReversed",
"back": "2backReversed",
"value": 21,
"count": 7
},
{
"_id": "608a5b31a3f9806de253727X",
"front": "2front2",
"back": "2back2",
"value": 34,
"count": 8
},
{
"_id": "608a5b31a3f9806de253729Y",
"front": "2frontReversed2",
"back": "2backReversed2",
"value": 42,
"count": 9
}
]
},
{
"_id": "608642db80a36336946620aa",
"userID": "user2",
"title": "title4",
"flashcardReversed": [
{
"_id": "608d5b290e635ece6828131X",
"front": "2front",
"back": "2back",
"value": 41,
"count": 10
},
{
"_id": "608t5b290e635ece6828161Y",
"front": "2frontReversed",
"back": "2backReversed",
"value": 54,
"count": 11
},
{
"_id": "608a5b31a3f9806de253526X",
"front": "2front2",
"back": "2back2",
"value": 63,
"count": 12
},
{
"_id": "608a5b31a3f9806de253326Y",
"front": "2frontReversed2",
"back": "2backReversed2",
"value": 29,
"count": 13
}
]
}
]
例如查询应该是这样的:
{
"userID": {"$eq": provided_user_id}
{"flashcardReversed.value" + "flashcardReversed.count"}: {"$eq": provided_value_plus_count}
}
是否可以使用 pymongo 来实现这一点?
【问题讨论】:
-
输出已损坏(字段后缺少一些逗号)但
_id对于所有三个文档都是相同的——假设它们应该是集合中的三个独立文档而不是数组1 个文档中的 3 个项目。另外:您是要匹配为用户找到的所有文档的总和,还是每个文档(您的数据表明 userID 可以显示在多个文档中)。请澄清一下。 -
json 无效,如果您可以使用 this 之类的东西来检查它
标签: python mongodb mongodb-query pymongo