Group into group and count using aggregation framework in MongoDB - mongodb

I am trying to group data within group with the count.
{
"_id" : ObjectId("59899846b5f5670840040b0b"),
"client_id" : "merlin",
"scope" : "eee",
"user_id" : "u",
"user_directory" : "kmdkcn",
"createdAt" : ISODate("2017-08-08T10:53:58.816Z"),
"open_url" : null,
"__v" : 0
}
{
"_id" : ObjectId("59899849b5f5670840040b0c"),
"client_id" : "merlin",
"scope" : "eee",
"user_id" : "u",
"user_directory" : "kmdkcn",
"createdAt" : ISODate("2017-08-08T10:54:01.908Z"),
"open_url" : null,
"__v" : 0
}
{
"_id" : ObjectId("5989984db5f5670840040b0d"),
"client_id" : "merlin",
"scope" : "eee",
"user_id" : "y",
"user_directory" : "kmdkcn",
"createdAt" : ISODate("2017-08-08T10:54:05.280Z"),
"open_url" : null,
"__v" : 0
}
{
"_id" : ObjectId("5989adb2d699bd211caa07ad"),
"client_id" : "symphony",
"scope" : "eee",
"user_id" : "q",
"user_directory" : "kmdkcn",
"createdAt" : ISODate("2017-08-08T12:25:22.518Z"),
"open_url" : null,
"__v" : 0
}
{
"_id" : ObjectId("5989adb8d699bd211caa07ae"),
"client_id" : "symphony",
"scope" : "eee",
"user_id" : "w",
"user_directory" : "kmdkcn",
"createdAt" : ISODate("2017-08-08T12:25:28.954Z"),
"open_url" : null,
"__v" : 0
}
{
"_id" : ObjectId("5989adbcd699bd211caa07af"),
"client_id" : "symphony",
"scope" : "eee",
"user_id" : "q",
"user_directory" : "kmdkcn",
"createdAt" : ISODate("2017-08-08T12:25:32.753Z"),
"open_url" : null,
"__v" : 0
}
{
"_id" : ObjectId("5989adc0d699bd211caa07b0"),
"client_id" : "symphony",
"scope" : "eee",
"user_id" : "r",
"user_directory" : "kmdkcn",
"createdAt" : ISODate("2017-08-08T12:25:36.176Z"),
"open_url" : null,
"__v" : 0
}
What i want is to group data on the basis of client_id and further on the basis of user_id to get the count of documents.
i have tried this:-
Logger.aggregate([
{ "$group": {
"_id": {client: "$client_id"},
"count": { "$sum": 1 }
}}
])
I am getting:-
client: merlin
count: 3,
client: symphony,
count: 4
But how to group this on the basis of user_id.
I want the final output would be:-
client: merlin,
count: 2,
client: symphony,
count: 3

Here is the query.
db.collection.aggregate([
{ "$group": {
"_id": {client: "$client_id" },
"clientIdCount": { "$sum": 1 },
"userids" : {"$addToSet" : "$user_id"}
}
},
{ "$project" : {"_id.client" : 1, "clientIdCount" : 1, numberOfUsers : {$size : "$userids"} }
}
]);
Output:-
If you don't want clientIdCount in the output, just change the value from 1 to 0 present next to clientIdCount.
/* 1 */
{
"_id" : {
"client" : "symphony"
},
"clientIdCount" : 4,
"numberOfUsers" : 3
}
/* 2 */
{
"_id" : {
"client" : "merlin"
},
"clientIdCount" : 3,
"numberOfUsers" : 2
}

If I understood your question correctly this your db query
db.Logger.aggregate([
{
$group : {
_id : "$client_id", user : { $push: "$user_id" },
count: { $sum: 1 }
}
}
])

Related

Find and sort array of array in nested documents

My collection schema is like this:
{
"_id" : ObjectId("5f0c64e4dd0a36b93c7deafa"),
"name" : "Asd",
"email" : "asd#asd.com",
"password" : "$2b$12$66OTK8mSWELMF5YiF9HMUuHEeOVLI61aINjWs1Cmn1699lLJfz/7y",
"auto_ml" : true,
"notification" : true,
"photo" : null,
"tariff_id" : NumberInt(1),
"city" : null,
"sub_district" : null,
"village" : null,
"latitude" : null,
"longitude" : null,
"created_at" : ISODate("2020-07-13T20:43:00.871+0000"),
"updated_at" : ISODate("2020-07-13T23:08:26.149+0000"),
"family_members" : [
],
"rooms" : [
{
"_id" : ObjectId("5f0c98826f0321f6986755da"),
"name" : "Ruang Makan",
"created_at" : ISODate("2020-07-14T00:23:14.839+0000"),
"updated_at" : ISODate("2020-07-14T00:23:14.840+0000"),
"devices" : [
]
},
{
"_id" : ObjectId("5f0c98876f0321f6986755dd"),
"name" : "Ruang Tamu",
"created_at" : ISODate("2020-07-14T00:23:19.693+0000"),
"updated_at" : ISODate("2020-07-14T19:00:08.281+0000"),
"devices" : [
{
"serial_number" : "ST9L0CY4A2AVY7HFWWUE",
"used_relay" : NumberInt(0),
"created_at" : ISODate("2020-07-14T16:56:22.156+0000"),
"updated_at" : ISODate("2020-07-14T16:56:22.156+0000"),
"sensors" : [
{
"_id" : ObjectId("5f0db478ca203bde99d2438e"),
"name" : "Temperature",
"value" : null,
"created_at" : ISODate("2020-07-14T20:34:48.134+0000"),
"updated_at" : ISODate("2020-07-14T20:34:48.134+0000")
},
{
"_id" : ObjectId("5f0dbe0563ccbcb2a2aecc04"),
"name" : "Motion",
"value" : null,
"created_at" : ISODate("2020-07-14T21:15:33.135+0000"),
"updated_at" : ISODate("2020-07-14T21:15:33.135+0000")
},
{
"_id" : ObjectId("5f0dc0412022e93af338316f"),
"name" : "Humidity",
"value" : null,
"created_at" : ISODate("2020-07-14T21:25:05.126+0000"),
"updated_at" : ISODate("2020-07-14T21:25:05.126+0000")
},
{
"_id" : ObjectId("5f0dc0442022e93af3383170"),
"name" : "Light",
"value" : null,
"created_at" : ISODate("2020-07-14T21:25:08.451+0000"),
"updated_at" : ISODate("2020-07-14T21:25:08.451+0000")
}
],
"switches" : [
]
}
]
}
]
}
The question is, how could i access the array sensors inside device with "serial_number": "ST9L0CY4A2AVY7HFWWUE" and inside room with id ObjectId("5f0c98876f0321f6986755dd"), and the sort the sensors by id? I've tried the projection way and aggregate way but the result is not as i expected. My expected result is only showing the array of sensors like:
"sensors" : [
{
"_id" : ObjectId("5f0db478ca203bde99d2438e"),
"name" : "Temperature",
"value" : null,
"created_at" : ISODate("2020-07-14T20:34:48.134+0000"),
"updated_at" : ISODate("2020-07-14T20:34:48.134+0000")
},
{
"_id" : ObjectId("5f0dbe0563ccbcb2a2aecc04"),
"name" : "Motion",
"value" : null,
"created_at" : ISODate("2020-07-14T21:15:33.135+0000"),
"updated_at" : ISODate("2020-07-14T21:15:33.135+0000")
},
{
"_id" : ObjectId("5f0dc0412022e93af338316f"),
"name" : "Humidity",
"value" : null,
"created_at" : ISODate("2020-07-14T21:25:05.126+0000"),
"updated_at" : ISODate("2020-07-14T21:25:05.126+0000")
},
{
"_id" : ObjectId("5f0dc0442022e93af3383170"),
"name" : "Light",
"value" : null,
"created_at" : ISODate("2020-07-14T21:25:08.451+0000"),
"updated_at" : ISODate("2020-07-14T21:25:08.451+0000")
}
],
Query that i have tried:
db.users.aggregate([
{
$match: { "_id": ObjectId("5f0c64e4dd0a36b93c7deafa") }
},
{ $unwind: "$rooms" },
{
$match: {
"rooms._id": ObjectId("5f0c98876f0321f6986755dd")
}
},
{ $unwind: "$rooms.devices" },
{
$match: {
"rooms.devices.serial_number": "ST9L0CY4A2AVY7HFWWUE"
}
},
{
$project: { "rooms.devices.sensors": 1 }
},
{
$group: {
_id: "$_id",
sensors: { $first: "$rooms.devices.sensors" }
}
},
{
$sort: { "rooms.devices.sensors._id": -1}
},
])
But the sensors doesn't seem to be sorted.
Thanks in advance for your help~

How to sum this this JSON data values below

I am Imran, Please see the JSON data below and tell how to sum the result below:
{
"_id" : "59ae33130d83f7001112ea5e",
"user" : {
"passcode" : "****",
"operator" : "premiereservices",
"phone_num" : "8587366808",
"balance" : 1.99
},
"location_id" : "58ec4e82addba5001160fc07",
"customer_id" : "premiereservices",
"shopping_cart" : {
"item_count" : 2,
"items" : [
{
"customer_id" : "premiereservices",
"barcode" : "666",
"description" : "Test Item",
"price" : 1.99,
"taxable" : false,
"fixed_price_tax" : 0.05,
"__v" : 0
},
{
"customer_id" : "premiereservices",
"barcode" : "777",
"description" : "Test Item",
"price" : 1.99,
"taxable" : false,
"fixed_price_tax" : 0.05,
"__v" : 0
}
],
"total_due" : 4.08
},
"cart_total" : 4.08,
"Date" : "2017-09-04T23:16:03-06:00",
"__v" : 0
}
{
"_id" : "59ae38f186c7e70011a4e224",
"location_id" : "58ec522a99da86001123ec47",
"customer_id" : "premiereservices",
"user" : {
"phone_num" : "4129514766",
"balance" : 0.03
},
"cart_total" : 3.55,
"shopping_cart" : {
"items" : [
{
"barcode" : "028400020008",
"description" : "Smartfood White Cheddar Popcorn",
"price" : 1.25,
"taxable" : false,
"tax_collected" : 0
},
{
"barcode" : "012000286209",
"description" : "Pureleaf Unsweetened Tea",
"price" : 2.3,
"taxable" : false,
"tax_collected" : 0
}
]
},
"Date" : "2017-09-04T23:41:05-06:00",
"__v" : 0
}
{
"_id" : "59ae3c569ce8ec00112775d6",
"location_id" : "58ec522a99da86001123ec47",
"customer_id" : "premiereservices",
"user" : {
"phone_num" : "7192358363",
"balance" : 9.99
},
"cart_total" : 3.25,
"shopping_cart" : {
"items" : [
{
"barcode" : "10002",
"description" : "GnG Burrito Bacon/GChili/Potato",
"price" : 3.25,
"taxable" : true,
"tax_collected" : 0
}
]
},
"Date" : "2017-09-04T23:55:34-06:00",
"__v" : 0
}
Result Will be
cart_total: 10.88
price: 10.78
tax_collected: 0
Please anybody help me what will be the aggregate query functions
There you go:
db.collection.aggregate({
$project: { // for each document...
cart_total: 1, // ...keep the "cart_total" field
price: { $sum: "$shopping_cart.items.price" }, // ...sum up all "price" fields
tax_collected: { $sum: "$shopping_cart.items.tax_collected" } // ...sum up all "price" fields
}
}, {
$group: {
_id: null, // group all documents into the same bucket and sum up a bunch of values
cart_total: { $sum: "$cart_total" },
price: { $sum: "$price" },
tax_collected: { $sum: "$tax_collected" }
}
}, {
$project: {
_id: 0 // remove the "_id" field
}
})

Grouping in MongoDB Aggregate

I am starting with MongoDB and i find the Aggregate function hard to understand.
i have read many topics and tried many things, however i still don't get the results i am looking for.
I would like to group some things together to make a overview of the amount of things based on a few match parameters.
my DB:
{
"_id" : ObjectId("5943a4f015813165b6d2b79a"),
"productType" : "mail",
"productAmount" : 1,
"orderid" : "1497607274",
"location" : "99",
"sublocation" : "02",
"date" : ISODate("2017-01-01T09:01:13.000Z"),
"__v" : 0
}
/* 2 */
{
"_id" : ObjectId("5943a4f915813165b6d2b79c"),
"productType" : "mail",
"productAmount" : 1,
"orderid" : "1497607274",
"location" : "99",
"sublocation" : "01",
"date" : ISODate("2017-01-01T09:01:13.000Z"),
"__v" : 0
}
/* 3 */
{
"_id" : ObjectId("5943a4fe15813165b6d2b79e"),
"productType" : "mail",
"productAmount" : 1,
"orderid" : "1497607274",
"location" : "25",
"sublocation" : "01",
"date" : ISODate("2017-01-01T09:01:13.000Z"),
"__v" : 0
}
/* 4 */
{
"_id" : ObjectId("5943a56d15813165b6d2b7a0"),
"productType" : "mail",
"productAmount" : 1,
"orderid" : "1497607274",
"location" : "99",
"sublocation" : "01",
"date" : ISODate("2017-01-01T09:01:13.000Z"),
"__v" : 0
}
/* 5 */
{
"_id" : ObjectId("5943a971a367b069bb3e863c"),
"productType" : "mail",
"productAmount" : 1,
"orderid" : "1497607274",
"location" : "98",
"sublocation" : "01",
"date" : ISODate("2017-01-01T09:01:13.000Z"),
"__v" : 0
}
/* 6 */
{
"_id" : ObjectId("5943d824900fce1821c5845d"),
"productType" : "keychain",
"productAmount" : 1,
"orderid" : "1497607251",
"location" : "25",
"sublocation" : "01",
"date" : ISODate("2017-01-01T09:01:13.000Z")
}
/* 7 */
{
"_id" : ObjectId("5943d9ea900fce1821c5845e"),
"productType" : "mail",
"productAmount" : 1,
"orderid" : "1497607251",
"location" : "25",
"sublocation" : "02",
"date" : ISODate("2017-01-01T09:01:13.000Z")
}
my current query:
db.getCollection('products').aggregate([
{ $group: {
_id : {
"orderid" : "$orderid",
"location" : "$location",
"product" : "$productType",
"sublocation" : "$sublocation",
"date" : "$date",
"aant" : { "$sum" : "$productAmount"}
},
"aantal" : { "$sum" : 1},
"aantal2" : { "$sum" : "$productAmount"}
}
},
{
$group : {
_id : "$_id.product",
"productType" : {
"$push" : {
"loc" : "$_id.location",
"prodtype": "$_id.product",
"count": "$aantal2"},
},
"cnt": {"$sum" : "$aantal2"}
}
}
])
i would like to have this result:
{
Location: 25,
sublocation: 01,
<productTypeX> : <count the number of these products>,
<productTypeY> : <count the number of these products>,
<productTypeZ> : <count the number of these products>
},
{
Location: 25,
sublocation: 02,
<productTypeX> : <count the number of these products>,
<productTypeY> : <count the number of these products>,
<productTypeZ> : <count the number of these products>
}
or:
{
location: 25,
sublocation: 01,
products: [
{
<productTypeX> : <count>,
<productTypeY> : <count>,
<productTypeZ> : <count>
}
},
{
location: 25,
sublocation: 02
products: [
{
<productTypeX> : <count>,
<productTypeY> : <count>,
<productTypeZ> : <count>
}
}
if i get this to work i need to convert it to mongoose for NodeJS (tips are welcome)
Thanks in advance!

Aggregate inside document in mongodb

I have document sales
db.sale.findOne({_id : ObjectId("52ea4dd29dbc7923107ddb97")})
{
"_id" : ObjectId("52ea4dd29dbc7923107ddb97"),
"firm" : ObjectId("52e56c009dbc794999ea5c3d"),
"patient" : {
"last" : "",
"first" : ""
},
"doc" : "",
"hospital" : "",
"discount" : 0,
"dd" : "",
"mode" : "cash",
"invoice" : "300114-undefined-1",
"items" : [
{
"bat" : "BJFE",
"narco" : 0,
"name" : "GDRNCCD",
"mrp" : 1,
"free" : 0,
"qty" : 1,
"item_discount" : 0,
"wpr" : 1,
"exp" : "1425168000000"
},
{
"bat" : "",
"narco" : 0,
"name" : "GDRN vbhjdsfb",
"mrp" : 1,
"free" : 0,
"qty" : 1,
"item_discount" : 0,
"wpr" : 0,
"exp" : "[object Object]"
},
{
"bat" : "",
"narco" : 0,
"name" : "GDRN vbhjdsfb",
"mrp" : 1,
"free" : 0,
"qty" : 1,
"item_discount" : 0,
"wpr" : 0,
"exp" : "[object Object]"
}
],
"date" : ISODate("2014-01-30T00:00:00Z"),
"mob" : "",
"email" : ""
}
How can I Aggregate total numbers if items in one field and sum up mrp *qty of all the items into one field.
I have read the mognodb aggregation but it only aggregates among group of matched documents not inside a single document. Is it possile?
{
"_id" : ObjectId("52ea4dd29dbc7923107ddb97"),
"firm" : ObjectId("52e56c009dbc794999ea5c3d"),
'total_items' : items.length,
"total" : mrp*qty of all items,
}
db.sales.aggregate(
{$unwind: "$items"},
{$project: {_id: 1,firm:1, total: {$multiply: ["$items.mrp", "$items.qty"]}}},
{$group: {_id: {"id":"$_id", firm:"$firm"}, count: {$sum:1} , total : {$sum:"$total"}}}
)
With a slight change : _id contains id and firm, you will need an extra projection to match your desired document, but I don't think it's important.
Plus, you can easily change to group by farm only
Thanks to Orid,I tried this,
db.sale.aggregate(
{ $match :{ firm :ObjectId("52e56c009dbc794999ea5c3d") } },
{$project : {day : {$dayOfMonth : '$date'},items :1,patient :1,invoice :1}},
{$match : {day: {$gte : new Date().getDate()}}},
{$unwind : "$items"},
{$project: {_id: 1,patient:1,invoice :1, total: {$multiply: ["$items.mrp", "$items.qty"]}}},
{$group: {_id: {"id":"$_id", invoice:"$invoice",patient :"$patient"}, count: {$sum:1} , total : {$sum:"$total"}}})
Result
{
"result" : [
{
"_id" : {
"id" : ObjectId("52eab6129dbc7923107ddbaf"),
"invoice" : "310114-undefined-1",
"patient" : {
"last" : "",
"first" : ""
}
},
"count" : 2,
"total" : 25
},
{
"_id" : {
"id" : ObjectId("52eab6129dbc7923107ddbb0"),
"invoice" : "310114-undefined-1",
"patient" : {
"last" : "",
"first" : ""
}
},
"count" : 1,
"total" : 1
},
{
"_id" : {
"id" : ObjectId("52eab6129dbc7923107ddbae"),
"invoice" : "310114-undefined-1",
"patient" : {
"last" : "",
"first" : ""
}
},
"count" : 2,
"total" : 5
}
],
"ok" : 1
}

MongoDB groupby query

I have colletions containing records like
{ "type" : "me", "tid" : "1" }
{ "type" : "me", "tid" : "1" }
{ "type" : "me", "tid" : "1" }
{ "type" : "you", "tid" : "1" }
{ "type" : "you", "tid" : "1" }
{ "type" : "me", "tid" : "2" }
{ "type" : "me", "tid" : "2"}
{ "type" : "you", "tid" : "2"}
{ "type" : "you", "tid" : "2" }
{ "type" : "you", "tid" : "2"}
I have want result like below
[
{"tid" : "1","me" : 3,"you": 2},
{"tid" : "2","me" : 2,"you": 3}
]
I have tried group and; aggregate queries doesn't get required result format.
below is the group query.
db.coll.group({
key: {tid : 1,type:1},
cond: { tid : { "$in" : [ "1","2"]} },
reduce: function (curr,result) {
result.total = result.total + 1
},
initial: { total : 0}
})
it result is like
[
{"tid" : "1", "type" : "me" ,"total": 3 },
{"tid" : "1","type" : "you" ,"total": 2 },
{"tid" : "2", "type" : "me" ,"total": 2 },
{"tid" : "2","type" : "you" ,"total": 3 }
]
following is aggregate query
db.coll.aggregate([
{$match : { "tid" : {"$in" : ["1","2"]}}},
{$group : { _id : {tid : "$tid",type : "$type"},total : {"$sum" : 1}}}
])
gives following result
{
"result" :
[
{"_id" : {"tid" : "1","type" : "me"},"total" : 3},
{"_id" : {"tid" : "2","type" : "me" },"total" : 2},
{"_id" : {"tid" : "2","type" : "you"},"total" : 3}
]
"ok" : 1
}
it is possible to obtain I specified result or I have to do some manipulation in my code.
Thanks
If you change your aggregation to this:
db.so.aggregate([
{ $match : { "tid" : { "$in" : ["1", "2"] } } },
{ $group : {
_id : { tid : "$tid", type : "$type" },
total : { "$sum" : 1 }
} },
{ $group : {
_id : "$_id.tid",
values: { $push: { type: "$_id.type", total: '$total' } }
} }
])
Then your output is:
{
"result" : [
{
"_id" : "1",
"values" : [
{ "type" : "you", "total" : 2 },
{ "type" : "me", "total" : 3 }
]
},
{
"_id" : "2",
"values" : [
{ "type" : "me", "total" : 2 },
{ "type" : "you", "total" : 3 }
]
}
],
"ok" : 1
}
Although that is not the same as what you want, it is going to be the closest that you can get. And in your application, you can easily pull out the values in the same was as with what you would like to get out of it.
Just keep in mind, that in general you can not promote a value (you, me) to a key — unless your key is of a limited set (3-4 items max).