Getting a distinct aggregation of an array field across indexes

后端 未结 5 1509
[愿得一人]
[愿得一人] 2020-12-03 03:10

I\'m trying to learn MongoDB and how it\'d be useful for analytics for me. I\'m simply playing around with the JavaScript console available on their website and have created

相关标签:
5条回答
  • 2020-12-03 03:48

    You can use the aggregation framework. Depending on how you'd like the results structured, you can use either

    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
    }
    

    or

    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
    }
    
    0 讨论(0)
  • 2020-12-03 03:52

    You should be able to use this:

    db.mycollection.distinct("tags").sort()
    
    0 讨论(0)
  • 2020-12-03 03:55

    The code that fails on their website works on an actual MongoDB instance:

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

    Weird.

    0 讨论(0)
  • 2020-12-03 03:56

    Another way of getting unique array elements using aggregation pipeline

    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"}
              }}
            }
          }
        }}
      ]
    )
    

    collection

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

    pipeline

    >   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"}
    ...             }}
    ...           }
    ...         }
    ...       }}
    ...     ]
    ...   )
    

    result

    { "uniqueTags" : [ "abc", "def", "efg", "ijk", "lmo", "zyx" ] }
    
    0 讨论(0)
  • 2020-12-03 03:57

    There are couple of web mongo consoles available:

    • http://try.mongodb.org/
    • http://www.mongodb.org/#

    But if you type help in them you will realise they only support a very small number of 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
    

    As such distinct does not work because it is not supported.

    0 讨论(0)
提交回复
热议问题