1

I have entries in the shape of:

{
    outerField1 : {
                     "innerField" : 1
                 }
    outerField2 : "blah"
}

I'd like to sort them in descending order based on the innerField, but my understanding of the sort function is limited to knowing only how to sort based on the outer fields. How would I sort a mongo query based on an inner field?

4

1 回答 1

0

文档中的子字段可以通过使用点符号(例如outerfield1.innerField)来访问。

在 Mongo shell 中,您可以使用以下命令进行排序:

sort({'outerField1.innerField': -1})

如果您使用的是 Python,则可能需要改为:

sort([("outerField1.innerField": -1)])

你需要这样做的原因是 Python 的 dict 是一个无序的数据结构。详情见:https ://stackoverflow.com/a/10242305/117919

于 2013-07-18T19:42:10.050 回答