如何根据MongoDB中对象中的值对记录进行排序
请考虑以下MongoDB集合
[
{
_id: 123123,
name: "abc"
topic: {
asda: "Z"
}
},
{
_id: 123123,
name: "abc"
topic: {
dasd: "P"
}
},{
_id: 123123,
name: "abc"
topic: {
qwer: "A"
}
}
]
根据主题对象中的值进行了查询需求记录(casemensitiationality casemensitiationality ossensitiationality offictial otiven opivent otiven对象)之后,
。注意:主题对象内的密钥将始终是每个记录的不同唯一字符串,
因此结果记录应如下所示
[
{
_id: 123123,
name: "abc"
topic: {
qwer: "A"
}
},
{
_id: 123123,
name: "abc"
topic: {
dasd: "P"
}
},{
_id: 123123,
name: "abc"
topic: {
qwer: "Z"
}
}
]
::)
Consider the following MongoDB Collection
[
{
_id: 123123,
name: "abc"
topic: {
asda: "Z"
}
},
{
_id: 123123,
name: "abc"
topic: {
dasd: "P"
}
},{
_id: 123123,
name: "abc"
topic: {
qwer: "A"
}
}
]
After performing a query need records sorted(case-insensitively) based on the value inside the topic object.
Note: The key inside the topic object will be always a different and unique string for every record
So the result records should be as follows
[
{
_id: 123123,
name: "abc"
topic: {
qwer: "A"
}
},
{
_id: 123123,
name: "abc"
topic: {
dasd: "P"
}
},{
_id: 123123,
name: "abc"
topic: {
qwer: "Z"
}
}
]
Thanks in Advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要以未知键的值排序结果,则可以使用
$ objectToArray
在此游乐场示例
If you want to get the results sorted by the value of an unknown key, you can use
$objectToArray
As you can see on this playground example