我正在尝试通过匹配条件获取所有记录,然后希望使用pymongo聚合以排序格式显示结果 这是我的收藏集
[{
"userId" : "user1",
"name" : "USER 1",
"description":"User has mobile no.",
"contacts" : [
{
"type" : "home",
"number" : "1234"
},
{
"type" : "mobile",
"number" : "3456"
}
],
"contactTypes" : [
"home", "mobile"
]
},
{
"userId" : "user2",
"name" : "USER 2",
"description":"User has office and no mobile number",
"contacts" : [
{
"type" : "office",
"number" : "1111"
}
],
"contactTypes" : [
"office"
]
},
{
"userId" : "user3",
"name" : "USER 3",
"description":"User has office no",
"contacts" : [
{
"type" : "home",
"number" : "1111"
},
{
"type" : "office",
"number" : "2222"
}
],
"contactTypes" : [
"home", "office"
]
}]
现在,我想检索所有联系人类型为“移动”或描述中包含移动词的文档(这将返回前两个文档),并且还希望按contacts.number排序返回的结果。由于user2的contacts.number为“ 1111”,因此应首先显示如下:
"userId" : "user2",
"name" : "USER 2",
"description":"User has office and no mobile number",
"contacts" : [
{
"type" : "office",
"number" : "1111"
}
],
"contactTypes" : [
"office"
]
},{
"userId" : "user1",
"name" : "USER 1",
"description":"User has mobile no.",
"contacts" : [
{
"type" : "home",
"number" : "1234"
},
{
"type" : "mobile",
"number" : "3456"
}
],
"contactTypes" : [
"home", "mobile"
]
}]
所以我的查询如下:
db.collection.aggregate(
[
{
"$unwind": "contacts"
},
{
"$match": {
"$or": [
{
"description": {
"$regex": "mobile"
}
},
{
"contactTypes": {
"$regex": "mobile"
}
}
]
}
},
{
"$sort": {"contacts.number":1}
}
]
)
但是我遇到类似-“无法使用并行数组的键排序”的错误
不知道我在这里想念的是什么,或者应该如何写上面的查询。