2012-08-29 67 views
11

我正在嘗試學習MongoDB以及它對於我來說如何對分析有用。我只是玩弄JavaScript控制檯可以在他們的網站,並創造了以下項目:跨索引獲取數組字段的獨特聚合

{"title": "Cool", "_id": {"$oid": "503e4dc0cc93742e0d0ccad3"}, "tags": ["twenty", "sixty"]} 
{"title": "Other", "_id": {"$oid": "503e4e5bcc93742e0d0ccad4"}, "tags": ["ten", "thirty"]} 
{"title": "Ouch", "_id": {"$oid": "503e4e72cc93742e0d0ccad5"}, "tags": ["twenty", "seventy"]} 
{"title": "Final", "_id": {"$oid": "503e4e72cc93742e0d0ccad6"}, "tags": ["sixty", "seventy"]} 

我希望做的是查詢,所以我得到的獨特標籤的列表中的所有這些對象。結果應該是這樣的:

["ten", "twenty", "thirty", "sixty", "seventy"] 

我該如何查詢?我試圖distinct()它,但電話總是失敗,甚至沒有查詢。

回答

24

在其網站上出現故障代碼工作的實際MongoDB實例:

> db.posts.insert({title: "Hello", tags: ["one", "five"]}); 
> db.posts.insert({title: "World", tags: ["one", "three"]}); 
> db.posts.distinct("tags"); 
[ "one", "three", "five"] 

奇怪。

+1

你得到什麼錯誤訊息?我相信控制檯運行在一個非常古老的mongo版本上... – Sammaye

+0

這段代碼與Mongo 3.6.0完美搭配使用 – kopos

3

有可用的情侶網蒙戈控制檯:

但是,如果你在其中鍵入help你會意識到他們只支持極少數OPS的:

HELP 
Note: Only a subset of MongoDB's features are provided here. 
For everything else, download and install at mongodb.org. 

db.foo.help()     help on collection method 
db.foo.find()     list objects in collection foo 
db.foo.save({a: 1})   save a document to collection foo 
db.foo.update({a: 1}, {a: 2}) update document where a == 1 
db.foo.find({a: 1})   list objects in foo where a == 1 

it       use to further iterate over a cursor 

由於這樣的區別不因爲它不被支持而工作。

+0

@wkhatch如果你再次讀到這個問題,OP會詢問如何在Web控制檯中進行聚合,在MongoDB網站上。我正確地回答說你不能 – Sammaye

5

您可以使用聚合框架。根據您希望如何結構化的結果,您可以使用

var pipeline = [ 
     {"$unwind": "$tags" } , 
     { "$group": { _id: "$tags" } } 
    ]; 
R = db.tb.aggregate(pipeline); 
printjson(R); 

{ 
     "result" : [ 
       { 
         "_id" : "seventy" 
       }, 
       { 
         "_id" : "ten" 
       }, 
       { 
         "_id" : "sixty" 
       }, 
       { 
         "_id" : "thirty" 
       }, 
       { 
         "_id" : "twenty" 
       } 
     ], 
     "ok" : 1 
} 

var pipeline = [ 
     {"$unwind": "$tags" } , 
     { "$group": 
      { _id: null, tags: {"$addToSet": "$tags" } } 
     } 
    ]; 
R = db.tb.aggregate(pipeline); 
printjson(R); 

{ 
     "result" : [ 
       { 
         "_id" : null, 
         "tags" : [ 
           "seventy", 
           "ten", 
           "sixty", 
           "thirty", 
           "twenty" 
         ] 
       } 
     ], 
     "ok" : 1 
} 
7

您應該能夠使用:

db.mycollection.distinct("tags").sort() 
1

獲得獨特的另一種方式使用聚合流水線的陣列元素

db.blogs.aggregate(
    [ 
    {$group:{_id : null, uniqueTags : {$push : "$tags"}}}, 
    {$project:{ 
     _id : 0, 
     uniqueTags : { 
     $reduce : { 
      input : "$uniqueTags", 
      initialValue :[], 
      in : {$let : { 
      vars : {elem : { $concatArrays : ["$$this", "$$value"] }}, 
      in : {$setUnion : "$$elem"} 
      }} 
     } 
     } 
    }} 
    ] 
) 

收集

> db.blogs.find() 
{ "_id" : ObjectId("5a6d53faca11d88f428a2999"), "name" : "sdfdef", "tags" : [ "abc", "def", "efg", "abc" ] } 
{ "_id" : ObjectId("5a6d5434ca11d88f428a299a"), "name" : "abcdef", "tags" : [ "abc", "ijk", "lmo", "zyx" ] } 
> 

管道

> db.blogs.aggregate(
...  [ 
...  {$group:{_id : null, uniqueTags : {$push : "$tags"}}}, 
...  {$project:{ 
...   _id : 0, 
...   uniqueTags : { 
...   $reduce : { 
...    input : "$uniqueTags", 
...    initialValue :[], 
...    in : {$let : { 
...    vars : {elem : { $concatArrays : ["$$this", "$$value"] }}, 
...    in : {$setUnion : "$$elem"} 
...    }} 
...   } 
...   } 
...  }} 
...  ] 
... ) 

結果

{ "uniqueTags" : [ "abc", "def", "efg", "ijk", "lmo", "zyx" ] }