排序父字段未知的子字段 [英] Sort subfields with unknown parent
本文介绍了排序父字段未知的子字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的架构:
{
"_id" : "555",
"connections" : [
{
"id" : 111
"time" : 1456439249
},
{
"id" : 222
"time" : 1556412345
}
...
}
"users" : [
"111" : {
"id" : 111
"name" : "Michael"
},
"222" : {
"id" : 222
"name" : "Jim"
}
...
}
我想按时间获得已排序的连接和用户数据.
I want to get sorted connections by time and users data.
我正在尝试:
db.getCollection('mycollecion')
.find(
{'_id' : '555'},
{'_id' : 0, 'connections' : 1, 'users' : 1}
).sort({'connections.time' : 1})
"connections.time"可能不是正确的路径,因为它是数组.
Probably "connections.time" is not the correct path, because it's array.
如何按时间"子字段对连接进行排序?
是否有可能通过出现在连接ID上而出现在同一查询过滤器用户"中?
And it would be possible in the same query filter "users" by appearing on connections ids?
推荐答案
请尝试通过.aggregation()
> db.mycollecion.aggregate([
{$unwind: '$connections'},
{$sort: {'connections.time': 1}},
{$group: {
_id: '$_id',
connections: {$push: '$connections'}
}
}]);
这篇关于排序父字段未知的子字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文