MiwGates
MiwGates

Reputation: 13

mongodb text search, groupby and count

MongoDB data is as follows:

data1:
"_id" : ObjectId("5252a2d13cd306f74c01a3ce"),
"favorited" : false,
"user_id" : 311692506,
"source" : "web"
"text" : "help me!",
"created_at" : ISODate("2013-10-07T12:02:25.430Z"),

data2:
"_id" : ObjectId("527a5a943cd306e83e04071b"),
"favorited" : false,
"user_id" : 1635733694,
"source" : "Android",
"text" : "help 123",
"created_at" : ISODate("2013-11-06T15:04:52.066Z"),

data3:
"_id" : ObjectId("526d03183cd306f74c0f842c"),
"favorited" : false,
"user_id" : 302310143,
"text" : "help you are...",
"created_at" : ISODate("2013-10-27T12:12:08.819Z"),

data4:
"_id" : ObjectId("526030923cd306f74c08e89b"),
"favorited" : false,
"user_id" : 1155372614,
"source" : "Android",
"text" : "heeeey help",
"created_at" : ISODate("2013-10-17T18:46:42.977Z"),

data5:
"_id" : ObjectId("525401253cd306f74c02c235"),
"favorited" : false,
"user_id" : 302310143,
"source" : "IOS",
"text" : "help android help",
"created_at" : ISODate("2013-10-08T12:57:09.496Z"),

I also get it by using MongoDB TextSearch. I want to count similar source areas the result would be as follows:

IOS: 1
Android: 2
web: 1

my mongodb find code: db.mycollection.runCommand( "text", { search: "yardim", limit:5})

How do I integrate "aggregate" code to "find" code

Upvotes: 1

Views: 295

Answers (1)

Parvin Gasimzade
Parvin Gasimzade

Reputation: 26032

You can do it by using the aggregation framework. Code should be something like :

db.myObject.aggregate(
    {$match:{"source" : {$exists:true}}},
    {$group:{_id:"$source", "count": {$sum:1}}}
);

Upvotes: 1

Related Questions