Home > database >  Mongo can be a statistics a number of the document each value have filed
Mongo can be a statistics a number of the document each value have filed

Time:09-27

Such as I have a filed in a collection called "user", I want to a all users have the number of the document come out,
For example,
{user: "user1", filed1: "filed1"}
{user: "user1", filed1: "filed1"}
{user: "user1", filed1: "filed1"}
{user: "user2 filed2:" filed2 "}
{user: "user2 filed2:" filed2 "}

I query a can get user1 has 3, user2 have 2


For help

CodePudding user response:

http://docs.mongodb.org/manual/reference/method/db.collection.group/

Directing a group, but he is aggregation function need to implement a js

CodePudding user response:

Can directly use aggregate to implement
If a table is called a

Db. A.a ggregate (
{$group: {_id: "$user," num: {$sum: 1}}}
)

CodePudding user response:

Db. Collection. Aggregate ([{$group: {user: "$user," the count: {$sum: 1}}}]
Where user : "$user" best use _id

CodePudding user response:

Graphs can be used for processing the db. Collection. Graphs (function () {$emit {this user, 1}), function (key, values) {return Array. The sum (values)}, {out: 'new_temp}). The find ()
  • Related