MongoDB (Mongoose) Get ranking by key value [duplicate] - mongodb

I am playing around with MongoDB trying to figure out how to do a simple
SELECT province, COUNT(*) FROM contest GROUP BY province
But I can't seem to figure it out using the aggregate function. I can do it using some really weird group syntax
db.user.group({
"key": {
"province": true
},
"initial": {
"count": 0
},
"reduce": function(obj, prev) {
if (true != null) if (true instanceof Array) prev.count += true.length;
else prev.count++;
}
});
But is there an easier/faster way using the aggregate function?

This would be the easier way to do it using aggregate:
db.contest.aggregate([
{"$group" : {_id:"$province", count:{$sum:1}}}
])

I need some extra operation based on the result of aggregate function. Finally I've found some solution for aggregate function and the operation based on the result in MongoDB. I've a collection Request with field request, source, status, requestDate.
Single Field Group By & Count:
db.Request.aggregate([
{"$group" : {_id:"$source", count:{$sum:1}}}
])
Multiple Fields Group By & Count:
db.Request.aggregate([
{"$group" : {_id:{source:"$source",status:"$status"}, count:{$sum:1}}}
])
Multiple Fields Group By & Count with Sort using Field:
db.Request.aggregate([
{"$group" : {_id:{source:"$source",status:"$status"}, count:{$sum:1}}},
{$sort:{"_id.source":1}}
])
Multiple Fields Group By & Count with Sort using Count:
db.Request.aggregate([
{"$group" : {_id:{source:"$source",status:"$status"}, count:{$sum:1}}},
{$sort:{"count":-1}}
])

If you need multiple columns to group by, follow this model. Here I am conducting a count by status and type:
db.BusinessProcess.aggregate({
"$group": {
_id: {
status: "$status",
type: "$type"
},
count: {
$sum: 1
}
}
})

Starting in MongoDB 3.4, you can use the $sortByCount aggregation.
Groups incoming documents based on the value of a specified expression, then computes the count of documents in each distinct group.
https://docs.mongodb.com/manual/reference/operator/aggregation/sortByCount/
For example:
db.contest.aggregate([
{ $sortByCount: "$province" }
]);

Additionally if you need to restrict the grouping you can use:
db.events.aggregate(
{$match: {province: "ON"}},
{$group: {_id: "$date", number: {$sum: 1}}}
)

This type of query worked for me:
db.events.aggregate({$group: {_id : "$date", number: { $sum : 1} }} )
See http://docs.mongodb.org/manual/tutorial/aggregation-with-user-preference-data/

Starting in Mongo 5.0, we can also use { $count: { } } as an alias for { $sum : 1 }:
// { "province" : "Champagne-Ardenne" }
// { "province" : "Champagne-Ardenne" }
// { "province" : "Haute-Normandie" }
db.collection.aggregate([
{ $group: { _id: "$province", count: { $count: {} } } }
])
// { "_id" : "Champagne-Ardenne", "count" : 2 }
// { "_id" : "Haute-Normandie", "count" : 1 }

db.contest.aggregate([
{ $match:{.....May be some match criteria...}},
{ $project: {"province":1,_id:0}},
{ $sortByCount: "$province" }
],{allowDiskUse:true});
MongoDB have 32 MB limitation of sorting operation on memory, use allowDiskUse : true this option, when you expose this query upfront of millions of data, it will sort at disk level not in memory. MongoDB aggregation pipeline has 100MB limitation, so use $project to reduce the data flowing to next pipeline.
If you are using small data then no need to use allowDiskUse option.

Mongo shell command that worked for me:
db.getCollection(<collection_name>).aggregate([{"$match": {'<key>': '<value to match>'}}, {"$group": {'_id': {'<group_by_attribute>': "$group_by_attribute"}}}])

Related

How to group by and count with MongoDB? [duplicate]

I am playing around with MongoDB trying to figure out how to do a simple
SELECT province, COUNT(*) FROM contest GROUP BY province
But I can't seem to figure it out using the aggregate function. I can do it using some really weird group syntax
db.user.group({
"key": {
"province": true
},
"initial": {
"count": 0
},
"reduce": function(obj, prev) {
if (true != null) if (true instanceof Array) prev.count += true.length;
else prev.count++;
}
});
But is there an easier/faster way using the aggregate function?
This would be the easier way to do it using aggregate:
db.contest.aggregate([
{"$group" : {_id:"$province", count:{$sum:1}}}
])
I need some extra operation based on the result of aggregate function. Finally I've found some solution for aggregate function and the operation based on the result in MongoDB. I've a collection Request with field request, source, status, requestDate.
Single Field Group By & Count:
db.Request.aggregate([
{"$group" : {_id:"$source", count:{$sum:1}}}
])
Multiple Fields Group By & Count:
db.Request.aggregate([
{"$group" : {_id:{source:"$source",status:"$status"}, count:{$sum:1}}}
])
Multiple Fields Group By & Count with Sort using Field:
db.Request.aggregate([
{"$group" : {_id:{source:"$source",status:"$status"}, count:{$sum:1}}},
{$sort:{"_id.source":1}}
])
Multiple Fields Group By & Count with Sort using Count:
db.Request.aggregate([
{"$group" : {_id:{source:"$source",status:"$status"}, count:{$sum:1}}},
{$sort:{"count":-1}}
])
If you need multiple columns to group by, follow this model. Here I am conducting a count by status and type:
db.BusinessProcess.aggregate({
"$group": {
_id: {
status: "$status",
type: "$type"
},
count: {
$sum: 1
}
}
})
Starting in MongoDB 3.4, you can use the $sortByCount aggregation.
Groups incoming documents based on the value of a specified expression, then computes the count of documents in each distinct group.
https://docs.mongodb.com/manual/reference/operator/aggregation/sortByCount/
For example:
db.contest.aggregate([
{ $sortByCount: "$province" }
]);
Additionally if you need to restrict the grouping you can use:
db.events.aggregate(
{$match: {province: "ON"}},
{$group: {_id: "$date", number: {$sum: 1}}}
)
This type of query worked for me:
db.events.aggregate({$group: {_id : "$date", number: { $sum : 1} }} )
See http://docs.mongodb.org/manual/tutorial/aggregation-with-user-preference-data/
Starting in Mongo 5.0, we can also use { $count: { } } as an alias for { $sum : 1 }:
// { "province" : "Champagne-Ardenne" }
// { "province" : "Champagne-Ardenne" }
// { "province" : "Haute-Normandie" }
db.collection.aggregate([
{ $group: { _id: "$province", count: { $count: {} } } }
])
// { "_id" : "Champagne-Ardenne", "count" : 2 }
// { "_id" : "Haute-Normandie", "count" : 1 }
db.contest.aggregate([
{ $match:{.....May be some match criteria...}},
{ $project: {"province":1,_id:0}},
{ $sortByCount: "$province" }
],{allowDiskUse:true});
MongoDB have 32 MB limitation of sorting operation on memory, use allowDiskUse : true this option, when you expose this query upfront of millions of data, it will sort at disk level not in memory. MongoDB aggregation pipeline has 100MB limitation, so use $project to reduce the data flowing to next pipeline.
If you are using small data then no need to use allowDiskUse option.
Mongo shell command that worked for me:
db.getCollection(<collection_name>).aggregate([{"$match": {'<key>': '<value to match>'}}, {"$group": {'_id': {'<group_by_attribute>': "$group_by_attribute"}}}])

How to count the number of $groups in a aggregation? [duplicate]

I am playing around with MongoDB trying to figure out how to do a simple
SELECT province, COUNT(*) FROM contest GROUP BY province
But I can't seem to figure it out using the aggregate function. I can do it using some really weird group syntax
db.user.group({
"key": {
"province": true
},
"initial": {
"count": 0
},
"reduce": function(obj, prev) {
if (true != null) if (true instanceof Array) prev.count += true.length;
else prev.count++;
}
});
But is there an easier/faster way using the aggregate function?
This would be the easier way to do it using aggregate:
db.contest.aggregate([
{"$group" : {_id:"$province", count:{$sum:1}}}
])
I need some extra operation based on the result of aggregate function. Finally I've found some solution for aggregate function and the operation based on the result in MongoDB. I've a collection Request with field request, source, status, requestDate.
Single Field Group By & Count:
db.Request.aggregate([
{"$group" : {_id:"$source", count:{$sum:1}}}
])
Multiple Fields Group By & Count:
db.Request.aggregate([
{"$group" : {_id:{source:"$source",status:"$status"}, count:{$sum:1}}}
])
Multiple Fields Group By & Count with Sort using Field:
db.Request.aggregate([
{"$group" : {_id:{source:"$source",status:"$status"}, count:{$sum:1}}},
{$sort:{"_id.source":1}}
])
Multiple Fields Group By & Count with Sort using Count:
db.Request.aggregate([
{"$group" : {_id:{source:"$source",status:"$status"}, count:{$sum:1}}},
{$sort:{"count":-1}}
])
If you need multiple columns to group by, follow this model. Here I am conducting a count by status and type:
db.BusinessProcess.aggregate({
"$group": {
_id: {
status: "$status",
type: "$type"
},
count: {
$sum: 1
}
}
})
Starting in MongoDB 3.4, you can use the $sortByCount aggregation.
Groups incoming documents based on the value of a specified expression, then computes the count of documents in each distinct group.
https://docs.mongodb.com/manual/reference/operator/aggregation/sortByCount/
For example:
db.contest.aggregate([
{ $sortByCount: "$province" }
]);
Additionally if you need to restrict the grouping you can use:
db.events.aggregate(
{$match: {province: "ON"}},
{$group: {_id: "$date", number: {$sum: 1}}}
)
This type of query worked for me:
db.events.aggregate({$group: {_id : "$date", number: { $sum : 1} }} )
See http://docs.mongodb.org/manual/tutorial/aggregation-with-user-preference-data/
Starting in Mongo 5.0, we can also use { $count: { } } as an alias for { $sum : 1 }:
// { "province" : "Champagne-Ardenne" }
// { "province" : "Champagne-Ardenne" }
// { "province" : "Haute-Normandie" }
db.collection.aggregate([
{ $group: { _id: "$province", count: { $count: {} } } }
])
// { "_id" : "Champagne-Ardenne", "count" : 2 }
// { "_id" : "Haute-Normandie", "count" : 1 }
db.contest.aggregate([
{ $match:{.....May be some match criteria...}},
{ $project: {"province":1,_id:0}},
{ $sortByCount: "$province" }
],{allowDiskUse:true});
MongoDB have 32 MB limitation of sorting operation on memory, use allowDiskUse : true this option, when you expose this query upfront of millions of data, it will sort at disk level not in memory. MongoDB aggregation pipeline has 100MB limitation, so use $project to reduce the data flowing to next pipeline.
If you are using small data then no need to use allowDiskUse option.
Mongo shell command that worked for me:
db.getCollection(<collection_name>).aggregate([{"$match": {'<key>': '<value to match>'}}, {"$group": {'_id': {'<group_by_attribute>': "$group_by_attribute"}}}])

Mongodb- Get distinct count of documents where a field matches a particular value [duplicate]

I am playing around with MongoDB trying to figure out how to do a simple
SELECT province, COUNT(*) FROM contest GROUP BY province
But I can't seem to figure it out using the aggregate function. I can do it using some really weird group syntax
db.user.group({
"key": {
"province": true
},
"initial": {
"count": 0
},
"reduce": function(obj, prev) {
if (true != null) if (true instanceof Array) prev.count += true.length;
else prev.count++;
}
});
But is there an easier/faster way using the aggregate function?
This would be the easier way to do it using aggregate:
db.contest.aggregate([
{"$group" : {_id:"$province", count:{$sum:1}}}
])
I need some extra operation based on the result of aggregate function. Finally I've found some solution for aggregate function and the operation based on the result in MongoDB. I've a collection Request with field request, source, status, requestDate.
Single Field Group By & Count:
db.Request.aggregate([
{"$group" : {_id:"$source", count:{$sum:1}}}
])
Multiple Fields Group By & Count:
db.Request.aggregate([
{"$group" : {_id:{source:"$source",status:"$status"}, count:{$sum:1}}}
])
Multiple Fields Group By & Count with Sort using Field:
db.Request.aggregate([
{"$group" : {_id:{source:"$source",status:"$status"}, count:{$sum:1}}},
{$sort:{"_id.source":1}}
])
Multiple Fields Group By & Count with Sort using Count:
db.Request.aggregate([
{"$group" : {_id:{source:"$source",status:"$status"}, count:{$sum:1}}},
{$sort:{"count":-1}}
])
If you need multiple columns to group by, follow this model. Here I am conducting a count by status and type:
db.BusinessProcess.aggregate({
"$group": {
_id: {
status: "$status",
type: "$type"
},
count: {
$sum: 1
}
}
})
Starting in MongoDB 3.4, you can use the $sortByCount aggregation.
Groups incoming documents based on the value of a specified expression, then computes the count of documents in each distinct group.
https://docs.mongodb.com/manual/reference/operator/aggregation/sortByCount/
For example:
db.contest.aggregate([
{ $sortByCount: "$province" }
]);
Additionally if you need to restrict the grouping you can use:
db.events.aggregate(
{$match: {province: "ON"}},
{$group: {_id: "$date", number: {$sum: 1}}}
)
This type of query worked for me:
db.events.aggregate({$group: {_id : "$date", number: { $sum : 1} }} )
See http://docs.mongodb.org/manual/tutorial/aggregation-with-user-preference-data/
Starting in Mongo 5.0, we can also use { $count: { } } as an alias for { $sum : 1 }:
// { "province" : "Champagne-Ardenne" }
// { "province" : "Champagne-Ardenne" }
// { "province" : "Haute-Normandie" }
db.collection.aggregate([
{ $group: { _id: "$province", count: { $count: {} } } }
])
// { "_id" : "Champagne-Ardenne", "count" : 2 }
// { "_id" : "Haute-Normandie", "count" : 1 }
db.contest.aggregate([
{ $match:{.....May be some match criteria...}},
{ $project: {"province":1,_id:0}},
{ $sortByCount: "$province" }
],{allowDiskUse:true});
MongoDB have 32 MB limitation of sorting operation on memory, use allowDiskUse : true this option, when you expose this query upfront of millions of data, it will sort at disk level not in memory. MongoDB aggregation pipeline has 100MB limitation, so use $project to reduce the data flowing to next pipeline.
If you are using small data then no need to use allowDiskUse option.
Mongo shell command that worked for me:
db.getCollection(<collection_name>).aggregate([{"$match": {'<key>': '<value to match>'}}, {"$group": {'_id': {'<group_by_attribute>': "$group_by_attribute"}}}])

MongoDB SELECT COUNT GROUP BY

I am playing around with MongoDB trying to figure out how to do a simple
SELECT province, COUNT(*) FROM contest GROUP BY province
But I can't seem to figure it out using the aggregate function. I can do it using some really weird group syntax
db.user.group({
"key": {
"province": true
},
"initial": {
"count": 0
},
"reduce": function(obj, prev) {
if (true != null) if (true instanceof Array) prev.count += true.length;
else prev.count++;
}
});
But is there an easier/faster way using the aggregate function?
This would be the easier way to do it using aggregate:
db.contest.aggregate([
{"$group" : {_id:"$province", count:{$sum:1}}}
])
I need some extra operation based on the result of aggregate function. Finally I've found some solution for aggregate function and the operation based on the result in MongoDB. I've a collection Request with field request, source, status, requestDate.
Single Field Group By & Count:
db.Request.aggregate([
{"$group" : {_id:"$source", count:{$sum:1}}}
])
Multiple Fields Group By & Count:
db.Request.aggregate([
{"$group" : {_id:{source:"$source",status:"$status"}, count:{$sum:1}}}
])
Multiple Fields Group By & Count with Sort using Field:
db.Request.aggregate([
{"$group" : {_id:{source:"$source",status:"$status"}, count:{$sum:1}}},
{$sort:{"_id.source":1}}
])
Multiple Fields Group By & Count with Sort using Count:
db.Request.aggregate([
{"$group" : {_id:{source:"$source",status:"$status"}, count:{$sum:1}}},
{$sort:{"count":-1}}
])
If you need multiple columns to group by, follow this model. Here I am conducting a count by status and type:
db.BusinessProcess.aggregate({
"$group": {
_id: {
status: "$status",
type: "$type"
},
count: {
$sum: 1
}
}
})
Starting in MongoDB 3.4, you can use the $sortByCount aggregation.
Groups incoming documents based on the value of a specified expression, then computes the count of documents in each distinct group.
https://docs.mongodb.com/manual/reference/operator/aggregation/sortByCount/
For example:
db.contest.aggregate([
{ $sortByCount: "$province" }
]);
Additionally if you need to restrict the grouping you can use:
db.events.aggregate(
{$match: {province: "ON"}},
{$group: {_id: "$date", number: {$sum: 1}}}
)
This type of query worked for me:
db.events.aggregate({$group: {_id : "$date", number: { $sum : 1} }} )
See http://docs.mongodb.org/manual/tutorial/aggregation-with-user-preference-data/
Starting in Mongo 5.0, we can also use { $count: { } } as an alias for { $sum : 1 }:
// { "province" : "Champagne-Ardenne" }
// { "province" : "Champagne-Ardenne" }
// { "province" : "Haute-Normandie" }
db.collection.aggregate([
{ $group: { _id: "$province", count: { $count: {} } } }
])
// { "_id" : "Champagne-Ardenne", "count" : 2 }
// { "_id" : "Haute-Normandie", "count" : 1 }
db.contest.aggregate([
{ $match:{.....May be some match criteria...}},
{ $project: {"province":1,_id:0}},
{ $sortByCount: "$province" }
],{allowDiskUse:true});
MongoDB have 32 MB limitation of sorting operation on memory, use allowDiskUse : true this option, when you expose this query upfront of millions of data, it will sort at disk level not in memory. MongoDB aggregation pipeline has 100MB limitation, so use $project to reduce the data flowing to next pipeline.
If you are using small data then no need to use allowDiskUse option.
Mongo shell command that worked for me:
db.getCollection(<collection_name>).aggregate([{"$match": {'<key>': '<value to match>'}}, {"$group": {'_id': {'<group_by_attribute>': "$group_by_attribute"}}}])

MongoDB: count the number of items in an array

I have a collection where every document in the collection has an array named foo that contains a set of embedded documents. Is there currently a trivial way in the MongoDB shell to count how many instances are within foo? something like:
db.mycollection.foos.count() or db.mycollection.foos.size()?
Each document in the array needs to have a unique foo_id and I want to do a quick count to make sure that the right amount of elements are inside of an array for a random document in the collection.
In MongoDB 2.6, the Aggregation Framework has a new array $size operator you can use:
> db.mycollection.insert({'foo':[1,2,3,4]})
> db.mycollection.insert({'foo':[5,6,7]})
> db.mycollection.aggregate([{$project: { count: { $size:"$foo" }}}])
{ "_id" : ObjectId("5314b5c360477752b449eedf"), "count" : 4 }
{ "_id" : ObjectId("5314b5c860477752b449eee0"), "count" : 3 }
if you are on a recent version of mongo (2.2 and later) you can use the aggregation framework.
db.mycollection.aggregate([
{$unwind: '$foo'},
{$group: {_id: '$_id', 'sum': { $sum: 1}}},
{$group: {_id: null, total_sum: {'$sum': '$sum'}}}
])
which will give you the total foos of your collection.
Omitting the last group will aggregate results per record.
Using Projections and Groups
db.mycollection.aggregate(
[
{
$project: {
_id:0,
foo_count:{$size:"$foo"},
}
},
{
$group: {
foo_total:{$sum:"$foo_count"},
}
}
]
)
Multiple child array counts can also be calculated this way
db.mycollection.aggregate(
[
{
$project: {
_id:0,
foo1_count:{$size:"$foo1"},
foo2_count:{$size:"$foo2"},
}
},
{
$group: {
foo1_total:{$sum:"$foo1_count"},
foo2_total:{$sum:"$foo2_count"},
}
}
]
)