跨索引获取数组字段的明确聚合

时间:2012-08-29 17:31:17

标签: mongodb

我正在尝试学习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()它,但是在没有查询的情况下调用总是失败。

5 个答案:

答案 0 :(得分:28)

在其网站上失败的代码适用于实际的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 :(得分:9)

你应该能够使用它:

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

答案 2 :(得分:7)

您可以使用聚合框架。根据您对结果的结构,您可以使用

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
}

答案 3 :(得分:3)

有几种网络mongo控制台可用:

但是如果你输入帮助,你就会意识到他们只支持极少数的操作:

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

因为不支持,所以这种区别不起作用。

答案 4 :(得分: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" ] }