2014-02-11 54 views
0

我蒙戈集合包含諸如下列文件:蒙戈套疊的鍵排序

{ 
    "_id" : ObjectId("52de74863fcc41ddfc7b23a5"), 
    "ts" : "1385969614848", 
    "Info" : [ 
     { 
      "out" : 0, 
      "Type" : "1", 
      "Descr" : "Null0", 
     }, 
     { 
      "out" : 10, 
      "Type" : "1", 
      "Descr" : "Null", 
     }, 
     { 
      "out" : 20000, 
      "Type" : "1", 
      "Descr" : "Null0", 
     }, 
     { 
      "out" : 70, 
      "Type" : "10", 
      "Descr" : "abc", 
     } 
    ] 
} 
{ 
    "_id" : ObjectId("52de74863fcc41ddfc7b23a6"), 
    "ts" : "1385969614852", 
    "Info" : [ 
     { 
      "out" : 500, 
      "Type" : "1", 
      "Descr" : "Null0", 
     }, 
     { 
      "out" : 100, 
      "Type" : "1", 
      "Descr" : "Null", 
     }, 
     { 
      "out" : 2896, 
      "Type" : "1", 
      "Descr" : "Null0", 
     }, 
     { 
      "out" : 4052, 
      "Type" : "10", 
      "Descr" : "abc", 
     } 
    ] 
} 

我要排序的關鍵「走出去」。爲了做到這一點,我寫了下面蒙戈查詢:

db.collection_name.find({},{"Info.out":1}).sort({"Info.out":1}).pretty()

話,就說明下面的輸出:

{ 
    "_id" : ObjectId("52fa2922d73ddc832323f402"), 
    "Info" : [ 
     { 
      "out" : 0 
     }, 
     { 
      "out" : 10 
     }, 
     { 
      "out" : 20000 
     }, 
     { 
      "out" : 70 
     } 
    ] 
} 
{ 
    "_id" : ObjectId("52fa292ed73ddc832323f403"), 
    "Info" : [ 
     { 
      "out" : 500 
     }, 
     { 
      "out" : 100 
     }, 
     { 
      "out" : 2896 
     }, 
     { 
      "out" : 4052 
     } 
    ] 
} 

但是,我希望下面的輸出:

{ 
    "_id" : ObjectId("52fa2922d73ddc832323f402"), 
    "Info" : [ 
     { 
      "out" : 20000 
     }, 
     { 
      "out" : 70 
     }, 
     { 
      "out" : 10 
     }, 
     { 
      "out" : 0 
     } 
    ] 
} 
{ 
    "_id" : ObjectId("52fa292ed73ddc832323f403"), 
    "Info" : [ 
     { 
      "out" : 4052 
     }, 
     { 
      "out" : 2896 
     }, 
     { 
      "out" : 500 
     }, 
     { 
      "out" : 100 
     } 
    ] 
} 

有誰知道如何達到預期的輸出?

回答

3

使用以下aggregation framework操作:

db.collection.aggregate([ 
{$project: {_id:1, out: "$Info.out"} }, 
{$unwind: "$out"}, 
{$sort: {_id:1, "out":-1} }, 
{$group: {_id:"$_id" , "Info": { $push: {"out":"$out"}} } } 
]) 

生產:

{ 
    "result" : [ 
     { 
      "_id" : ObjectId("52de74863fcc41ddfc7b23a6"), 
      "Info" : [ 
       { 
        "out" : 4052 
       }, 
       { 
        "out" : 2896 
       }, 
       { 
        "out" : 500 
       }, 
       { 
        "out" : 100 
       } 
      ] 
     }, 
     { 
      "_id" : ObjectId("52de74863fcc41ddfc7b23a5"), 
      "Info" : [ 
       { 
        "out" : 20000 
       }, 
       { 
        "out" : 70 
       }, 
       { 
        "out" : 10 
       }, 
       { 
        "out" : 0 
       } 
      ] 
     } 
    ], 
    "ok" : 1 
} 

您可能還需要檢查:

db.collection.aggregate([ 
{$project: {_id:1, out: "$Info.out"} }, 
{$unwind: "$out"}, 
{$sort: {_id:1, "out":-1} }, 
{$group: {_id:"$_id" , "Info": { $push: "$out"} } } 
]) 

每個文檔在「潔淨輸出信息「array:

{ 
    "_id" : ObjectId("52de74863fcc41ddfc7b23a6"), 
    "Info" : [ 
     4052, 
     2896, 
     500, 
     100 
    ] 
} 
+0

它工作正常:) – Yogesh

+0

很高興幫助!請接受答案:) – Jinxcat

+0

我做了一個mongotry可視化輸出:http://mongotry.herokuapp.com/#?bookmarkId=52fb8dc14e86f9020071ed73 – bla