Mongo DB排序问题与升序

时间:2016-07-13 12:09:53

标签: mongodb sorting

我有一个名为" formTest123 "以下记录:

     /* 0 */
{
    "_id" : ObjectId("5784f5aeef31a98294231459"),
    "data" : [ 
        {
            "name" : "Amir",
            "Other" : [ 
                {
                    "data" : {
                        "city" : {
                            "address" : "pncjj"
                        }
                    }
                }, 
                {
                    "data" : {
                        "state" : {
                            "address" : "xyz"
                        }
                    }
                }
            ]
        }
    ]
}

/* 1 */
{
    "_id" : ObjectId("5784f62cef31a9829423145a"),
    "data" : [ 
        {
            "name" : "Zssmir",
            "Other" : [ 
                {
                    "data" : {
                        "city" : {
                            "address" : "bncd"
                        }
                    }
                }, 
                {
                    "data" : {
                        "state" : {
                            "address" : "gyk"
                        }
                    }
                }
            ]
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("5784f636ef31a9829423145b"),
    "data" : [ 
        {
            "name" : "Cmir",
            "Other" : [ 
                {
                    "data" : {
                        "city" : {
                            "address" : "tuhn"
                        }
                    }
                }, 
                {
                    "data" : {
                        "state" : {
                            "address" : "knm"
                        }
                    }
                }
            ]
        }
    ]
}

当我用以下内容查询此集合时:

db.formTest123.find().sort( { "data.Other.data.city.address" : -1})

表示按降序输出正确的输出:

    /* 0 */
{
    "_id" : ObjectId("5784f636ef31a9829423145b"),
    "data" : [ 
        {
            "name" : "Cmir",
            "Other" : [ 
                {
                    "data" : {
                        "city" : {
                            "address" : "tuhn"
                        }
                    }
                }, 
                {
                    "data" : {
                        "state" : {
                            "address" : "knm"
                        }
                    }
                }
            ]
        }
    ]
}

/* 1 */
{
    "_id" : ObjectId("5784f5aeef31a98294231459"),
    "data" : [ 
        {
            "name" : "Amir",
            "Other" : [ 
                {
                    "data" : {
                        "city" : {
                            "address" : "pncjj"
                        }
                    }
                }, 
                {
                    "data" : {
                        "state" : {
                            "address" : "xyz"
                        }
                    }
                }
            ]
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("5784f62cef31a9829423145a"),
    "data" : [ 
        {
            "name" : "Zssmir",
            "Other" : [ 
                {
                    "data" : {
                        "city" : {
                            "address" : "bncd"
                        }
                    }
                }, 
                {
                    "data" : {
                        "state" : {
                            "address" : "gyk"
                        }
                    }
                }
            ]
        }
    ]
}

但是当我查询时:

 db.formTest123.find().sort( { "data.Other.data.city.address" : 1})

按升序排列" city.address "它给出了:

    /* 0 */
    {
    "_id" : ObjectId("5784f5aeef31a98294231459"),
    "data" : [ 
        {
            "name" : "Amir",
            "Other" : [ 
                {
                    "data" : {
                        "city" : {
                            "address" : "pncjj"
                        }
                    }
                }, 
                {
                    "data" : {
                        "state" : {
                            "address" : "xyz"
                        }
                    }
                }
            ]
        }
    ]
}

/* 1 */
{
    "_id" : ObjectId("5784f62cef31a9829423145a"),
    "data" : [ 
        {
            "name" : "Zssmir",
            "Other" : [ 
                {
                    "data" : {
                        "city" : {
                            "address" : "bncd"
                        }
                    }
                }, 
                {
                    "data" : {
                        "state" : {
                            "address" : "gyk"
                        }
                    }
                }
            ]
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("5784f636ef31a9829423145b"),
    "data" : [ 
        {
            "name" : "Cmir",
            "Other" : [ 
                {
                    "data" : {
                        "city" : {
                            "address" : "tuhn"
                        }
                    }
                }, 
                {
                    "data" : {
                        "state" : {
                            "address" : "knm"
                        }
                    }
                }
            ]
        }
    ]
}

这显然是错误的,因为现在的记录没有按照#34; city.address"

按升序排序

任何人都可以猜出升序有什么问题吗?

2 个答案:

答案 0 :(得分:0)

这是因为它从“最低”到“最高”排序。

问题是,undefined和/或null的值低于任何现有数字。

按数组排序时,它会查找该数组中的所有文档并获取最小值,该值在文档中为“undefined”,其中包含state。

例如,如果您为所有文档添加具有足够高值的“city.address”,它将适用于升序。像这样:

        "Other" : [ 
            {
                "data" : {
                    "city" : {
                        "address" : "tuhn"
                    }
                }
            }, 
            {
                "data" : {
                    "state" : {
                        "address" : "knm"
                    }
                    "city" : {
                        "address" : "zzzzzzzzzzzzzzzzz"
                    }
                }
            }
        ]

注意:对于降序,它取最大值,因此它“有效”,因为任何city.address都会覆盖所有null和undefined。

如果您需要升序并且不想更改数据结构,我建议按顺序降序,然后以编程方式反转顺序,如果可能的话。

答案 1 :(得分:0)

由于字段"其他"是(子)文档的数组,我们必须指定子文档的索引。 根据相关指定的模式,以下工作。

db.formTest123.find().sort( { "data.Other.0.data.city.address" : 1});

db.formTest123.find().sort( { "data.Other.0.data.city.address" : -1});