Mongodb字段的排序数组值

时间:2018-06-25 18:14:07

标签: mongodb aggregation-framework

我正在尝试致电Mongo集合,以按标签对文档进行排序。标签似乎不在数组内排序。

db.mydb.aggregate({ "$sort": { "labels": 1 } }, { $project : { labels : 1 } }  ] )

似乎排序正确,但不一致。

我试图使用聚合的$ unwind操作。

db.mydb.aggregate( [ { "$unwind" : "$labels" }, { "$sort": { "labels": 1 } } , { $project : { labels : 1 } }  ] )

$ unwind返回的文档比我原来要多。

这里是返回的东西。

{ "_id" : ObjectId("5a467a7a60b259c81d540b10"), "labels" : "1234" }
{ "_id" : ObjectId("5ab11e5e60b23bc77334d8de"), "labels" : "1234" }
{ "_id" : ObjectId("5accb97560b2fc27cbf4380a"), "labels" : "1234" }
{ "_id" : ObjectId("5ad4a45060b29049eb74d9f5"), "labels" : "1234" }
{ "_id" : ObjectId("5ad4a4d260b29049eb74d9f6"), "labels" : "1234" }
{ "_id" : ObjectId("5ad767b860b2ee64d233cfa6"), "labels" : "1234" }
{ "_id" : ObjectId("5b103458e4b0ef67aa953453"), "labels" : "1234" }
{ "_id" : ObjectId("5ab11db160b23bc77334d8dd"), "labels" : "1234" }
{ "_id" : ObjectId("5b16c1c2e4b0c648894ff8d0"), "labels" : "3625" }
{ "_id" : ObjectId("5ab11db160b23bc77334d8dd"), "labels" : "BOO" }
{ "_id" : ObjectId("5ab11db160b23bc77334d8dd"), "labels" : "Dan" }
{ "_id" : ObjectId("5ab11db160b23bc77334d8d0"), "labels" : "Doll" }
{ "_id" : ObjectId("5a0079ffe4b064bfa04b2082"), "labels" : "HELLO" }
{ "_id" : ObjectId("5aa91f4b60b24a721ddfbcaf"), "labels" : "Jay" }
{ "_id" : ObjectId("5ac5286760b208ad16315199"), "labels" : "John" }
{ "_id" : ObjectId("5a565ab060b25f8f58c82026"), "labels" : "Ray" }
{ "_id" : ObjectId("5a33cdba60b273ea196bb70e"), "labels" : "Miguel" }
{ "_id" : ObjectId("5a33cdc160b273ea196bb70f"), "labels" : "Miguel" }
{ "_id" : ObjectId("5a3995ac60b264b71aa8f3cc"), "labels" : "Miguel" }
{ "_id" : ObjectId("5a53996a60b2db6c14ee73db"), "labels" : "Miguel" }

这些值正在排序,但正在作为一个新文档添加。以下是我在Mongo收藏集中的文档样本。

acm: {...…}
created_by_dn: "...."
created_dt: "2018-06-21T16:13:55.405+0000"
labels: ["Dan", "1234", "BOO"]
original_object: {object_id: "5ab11db160b23bc77334d8dd", object_service: "foo_service", object_type: "foo_type",…}
object_id: "food_id"
object_service: "foo_service"
object_type: "foo_type"
uri: "foo_uri"
title: "foo_title"
type: "foo_type"

0 个答案:

没有答案