5

I have a mongodb collection which has collection with a tag and date field.

I want to count the number of all tags which can be done by this query

db.collection.count( { tags: "abc" })

But I would like to get counts of all unique tags together. And I also want to put a where condition on the date, as in I want to query for a certain time period.

4

3 回答 3

4

A very simple approach:

db.collection.distinct("tags", {dateField: {$gt: dateValue}}).forEach(function (tag) {
    var count = db.collection.count({"tags" : tag})
})
于 2013-03-30T09:20:13.313 回答
1

You can use the mongoDD Aggregation framework for solving this problem (http://docs.mongodb.org/manual/applications/aggregation/) . In case you do not get everything done . you always a option to do it through map-reduce (http://docs.mongodb.org/manual/applications/map-reduce/) . I have used map-reduce to build my own search options for special requirement. So any point of time map-reduce will help you to do what you want to do which is not possible by simple query. I am not giving the query because I do not have much information how you want to get the data and how is your collection looks like but both the two option will help you to get it very easily.

于 2013-03-30T05:31:46.590 回答
1

You should use a combination of find and count

db.collection.find({condition}).count
于 2018-02-22T02:36:43.347 回答