2012-08-29 26 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" ] }