How to put the Mongodb results into an array? - mongodb

I have a collection inside my database MongoDB:
/*Order:*/
{
item: 'banana',
price: '2$',
order_type: INTERNATIONAL // There are 2 types: INTERNATIONAL and MAINLAND
}
I do a group stage to group out all the orders with type INTERNATIONAL and MAINLAND then count the total number of each type of order.
db.getCollection('get_card_request').aggregate([
{$group: {
_id: '$order_type',
nbOfOrders: {$sum: 1}
}}
])
The results I received is like this:
/* 1 */
{
"_id" : "MAINLAND",
"nbOfOrder" : 3.0
}
/* 2 */
{
"_id" : "INTERNATIONAL",
"nbOfOrder" : 2.0
}
I want to put the results after the group stage inside an array but I don't know how to. I want to have a result like this:
{
orderTypeCount: [
{
"_id" : "MAINLAND",
"nbOfOrder" : 3.0
},
{
"_id" : "INTERNATIONAL",
"nbOfOrder" : 2.0
}
],
... // the other fields
}

play
db.collection.aggregate([
{
$group: {
_id: "$order_type",
nbOfOrders: {
$sum: 1
}
}
},
{
"$group": {
"_id": null,
"orderTypeCount": {
"$push": "$$ROOT"
}
}
},
{
$project: {
"_id": 0
}
}
])
I pushed all the data from previous pipeline to an array variable.

Related

mongo count rows from an array of provided data

I have collection like this:
{
"_id" : ObjectId("4d663451d1e7242c4b68e000"),
"topic" : "abc",
"subLevel" : {
"id" : 1
}
}
{
"_id" : ObjectId("4d6634514cb5cb2c4b69e000"),
"topic" : "bce",
"subLevel" : {
"id" : 1
}
}
{
"_id" : ObjectId("4d6634514cb5cb2c4b70e000"),
"topic" : "bec",
"subLevel" : {
"id" : 2
}
}
{
"_id" : ObjectId("4d6634514cb5cb2c4b70e000"),
"topic" : "vvv",
"subLevel" : {
"id" : 3
}
}
and I need to count how many documents exist for provided subLevel.id list, for example if I provide 1 and 2 it should show me that for 1 we have 2 documents and for 2 only 1 document and simply omit document where subLevel.id is 3 as it's not in the list of id's.
I tried to do it with a aggregate
db.getCollection('products').aggregate( [
{ $project:
{ "has_sublevel" : {$in: [ "subLevel.id", [1 , 2 ]]} }
},
{ $group: { _id : "$subLevel.id", count: { $sum: 1 } } }
] )
but result is
{
_id : null,
count: 4
}
how can I do it, thanks in advance!
If transform it to SQL which I familiar more, query should look like this:
select subLevelId, count(id) FROM products where subLevelId in (1,2) group by subLevelId
If I've understand correctly, you are so so close, check this query:
First use $match to get only documents whose subLevel.id is 1 or 2.
Then, as you have done, $group by the id and sum to get total count:
db.collection.aggregate([
{
"$match": { "subLevel.id": { "$in": [ 1, 2 ] } }
},
{
"$group": { "_id": "$subLevel.id", "count": { "$sum": 1 } }
}
])
Example here
You will need this:
db.products.aggregate([ {$match:{ "subLevel.id":{ $in:[1,2] } }} , {$group:{ _id:"$subLevel.id" , count:{$sum:1} } } ])
which is same like:
db.products.aggregate([ {$match:{ $or:[{"subLevel.id":1},{"subLevel.id":2} ]}} , {$group:{ _id:"$subLevel.id" , count:{$sum:1} } } ])
You need to push the respective docs into their respective arrays and then get their sizes:
db.collection.aggregate([
{
"$match": {
"subLevel.id": {
$in: [
1,
2
]
}
}
},
{
$group: {
"_id": "$subLevel.id",
ids: {
$push: "$_id"
}
}
},
{
$project: {
_id: false,
ids: {
$size: "$ids"
}
}
}
])
Playgroud: https://mongoplayground.net/p/J1ei37l1K5-

MongoDB two groups Aggregate

Aggregation operations process data records and return computed results. Aggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result. MongoDB provides three ways to perform aggregation: the aggregation pipeline, the map-reduce function, and single purpose aggregation methods.
I would like to transform that :
{
"_id" : ObjectId("5836b919885383034437d4a7"),
"Identificador" : "G-3474",
"Miembros" : [
{
"_id" : ObjectId("5836b916885383034437d238"),
"Nombre" : "Pilar",
"Email" : "pcarrillocasa#gmail.es",
"Edad" : 24,
"País" : "España",
"Tipo" : "Usuario individual",
"Apellidos" : "Carrillo Casa",
"Teléfono" : 637567234,
"Ciudad" : "Santander",
"Identificador" : "U-3486",
"Información_creación" : {
"Fecha_creación" : {
"Mes" : 4,
"Día" : 22,
"Año" : 2016
},
"Hora_creación" : {
"Hora" : 15,
"Minutos" : 34,
"Segundos" : 20
}
}
}
}
into that
{
"Nombre_Grupo" : "Amigo invisible"
"Ciudades" : [
{
"Ciudad" : "Madrid",
"Miembros": 30
},
{
"Ciudad" : "Almería",
"Miembros": 10
}
{
"Ciudad" : "Badajoz",
"Miembros": 20
}
]
}
with MongoDB.
I tried with that:
db.Grupos_usuarios.aggregate([
{ $group: { _id: "$Nombre_Grupo",total: { $sum: "$amount" } },
$group: { _id: "$Ciudad",total: { $sum: "$amount" } } }
])
but I could not get what I needed.
May somebody help me to know what I am doing bad?
The following aggregation gets the output you are looking for.
The $unwind stage deconstructs an array field from the input documents to output a document for each element. These documents are used to group by the Miembros.Ciudad and get the total Miembros for each Ciudad. In the second group stage we Pivot data to get all the Ciudades from the previous grouping into an array. The last $project is for formatting the output.
db.test.aggregate( [
{
$unwind: "$Miembros"
},
{
$group: {
_id: "$Miembros.Ciudad",
total: { $sum: 1 }
}
},
{
$group: {
_id: "Amigo invisible",
Ciudades: { $push: { Ciudad: "$_id", Miembros: "$total"} }
}
},
{
$project: {
Nombre_Grupo: "$_id",
Ciudades: 1,
_id: 0
}
}
] )

Count repeated objets in document in mongo

I have this type of data where i want to count repeated objects according to string
{
"version":"2.0",
"type":"DevicesSeen",
"data":{
"observations":[
{
"manufacturer":"Apple"
},
{
"manufacturer":"GUANGDONG OPPO MOBILE..."
},
{
"manufacturer":"Apple"
}
]
}
}
I want count of the manufacturer
example
Apple :2
Samsung :1
Try this one
db.collection.aggregate([
{
$unwind: "$data.observations"
},
{
$group: {
_id: "$data.observations.manufacturer",
counts: {$sum :1}
}
}
])
The result will be as below:
/* 1 */
{
"_id" : "GUANGDONG OPPO MOBILE...",
"counts" : 1
},
/* 2 */
{
"_id" : "Apple",
"counts" : 2
}

Need to sum from array object value in mongodb

I am trying to calculate total value if that value exits. But query is not working 100%. So can somebody help me to solve this problem. Here my sample document. I have attached two documents. Please these documents & find out best solution
Document : 1
{
"_id" : 1"),
"message_count" : 4,
"messages" : {
"data" : [
{
"id" : "11",
"saleValue": 1000
},
{
"id" : "112",
"saleValue": 1400
},
{
"id" : "22",
},
{
"id" : "234",
"saleValue": 111
}
],
},
"createdTime" : ISODate("2018-03-18T10:18:48.000Z")
}
Document : 2
{
"_id" : 444,
"message_count" : 4,
"messages" : {
"data" : [
{
"id" : "444",
"saleValue" : 2060
},
{
"id" : "444",
},
{
"id" : 234,
"saleValue" : 260
},
{
"id" : "34534",
}
]
},
"createdTime" : ISODate("2018-03-18T03:11:50.000Z")
}
Needed Output:
{
total : 4831
}
My query :
db.getCollection('myCollection').aggregate([
{
"$group": {
"_id": "$Id",
"totalValue": {
$sum: {
$sum: "$messages.data.saleValue"
}
}
}
}
])
So please if possible help me to solve this problem. Thanks in advance
It's not working correctly because it is aggregating all the documents in the collection; you are grouping on a constant "_id": "tempId", you just need to reference the correct key by adding the $ as:
db.getCollection('myCollection').aggregate([
{ "$group": {
"_id": "$tempId",
"totalValue": {
"$sum": { "$sum": "$messages.data.saleValue" }
}
} }
])
which in essence is a single stage pipeline version of an aggregate operation with an extra field that holds the sum expression before the group pipeline then calling that field as the $sum operator in the group.
The above works since $sum from MongoDB 3.2+ is available in both the $project and $group stages and when used in the $project stage, $sum returns the sum of the list of expressions. The expression "$messages.data.value" returns a list of numbers [120, 1200] which are then used as the $sum expression:
db.getCollection('myCollection').aggregate([
{ "$project": {
"values": { "$sum": "$messages.data.value" },
"tempId": 1,
} },
{ "$group": {
"_id": "$tempId",
"totalValue": { "$sum": "$values" }
} }
])
You can add a $unwind before your $group, in that way you will deconstructs the data array, and then you can group properly:
db.myCollection.aggregate([
{
"$unwind": "$messages.data"
},
{
"$group": {
"_id": "tempId",
"totalValue": {
$sum: {
$sum: "$messages.data.value"
}
}
}
}
])
Output:
{ "_id" : "tempId", "totalValue" : 1320 }
db.getCollection('myCollection').aggregate([
{
$unwind: "$messages.data",
$group: {
"_id": "tempId",
"totalValue": { $sum: "$messages.data.value" }
}
}
])
$unwind
According to description as mentioned into above question, as a solution please try executing following aggregate query
db.myCollection.aggregate(
// Pipeline
[
// Stage 1
{
$unwind: {
path: '$messages.data'
}
},
// Stage 2
{
$group: {
_id: {
pageId: '$pageId'
},
total: {
$sum: '$messages.data.saleValue'
}
}
},
// Stage 3
{
$project: {
pageId: '$_id.pageId',
total: 1,
_id: 0
}
}
]
);
You can do it without using $group. Grouping made other data to be managed and addressed. So, I prefer using $sum and $map as shown below:
db.getCollection('myCollection').aggregate([
{
$addFields: {
total: {
$sum: {
$map: {
input: "$messages.data",
as: "message",
in: "$$message.saleValue",
},
},
},
},
},
}
])

How to use nested grouping in MongoDB

I need to find total count of duplicate profiles per organization level. I have documents as shown below:
{
"OrganizationId" : 10,
"Profile" : {
"_id" : "75"
}
"_id" : "1"
},
{
"OrganizationId" : 10,
"Profile" : {
"_id" : "75"
}
"_id" : "2"
},
{
"OrganizationId" : 10,
"Profile" : {
"_id" : "77"
}
"_id" : "3"
},
{
"OrganizationId" : 10,
"Profile" : {
"_id" : "77"
}
"_id" : "4"
}
I have written query which is a group by ProfileId and OrganizationId. The results i am getting as shown below:
Organization Total
10 2
10 2
But i want to get the sum of total per organization level, that means Org 10 should have one row with sum of 4.
The query i am using as shown below:
db.getSiblingDB("dbName").OrgProfile.aggregate(
{ $project: { _id: 1, P: "$Profile._id", O: "$OrganizationId" } },
{ $group: {_id: { p: "$P", o: "$O"}, c: { $sum: 1 }} },
{ $match: { c: { $gt: 1 } } });
Any ideas ? Please help
The following pipeline should give you the desired output, whereas the last $project stage is just for cosmetic purposes to turn _id into OrganizationId but is not needed for the essential computation so you may omit it.
db.getCollection('yourCollection').aggregate([
{
$group: {
_id: { org: "$OrganizationId", profile: "$Profile._id" },
count: { $sum: 1 }
}
},
{
$group: {
_id: "$_id.org",
Total: {
$sum: {
$cond: {
if: { $gte: ["$count", 2] },
then: "$count",
else: 0
}
}
}
}
},
{
$project: {
_id: 0,
Organization: "$_id",
Total: 1
}
}
])
gives this output
{
"Total" : 4.0,
"Organization" : 10
}
To filter out organizations without duplicates you can use $match which will also result in a simplification of the second $group stage
...aggregate([
{
$group: {
_id: { org: "$OrganizationId", profile: "$Profile._id" },
count: { $sum: 1 }
}
},
{
$match: {
count: { $gte: 2 }
}
},
{
$group: {
_id: "$_id.org",
Total: { $sum: "$count" }
}
},
{
$project: {
_id: 0,
Organization: "$_id",
Total: 1
}
}
])
I think I have a solution for you. In that last step there, instead of matching, I think you want another $group.
.aggregate([
{ $project: { _id: 1, P: "$Profile._id", O: "$OrganizationId" } }
,{ $group: {_id: { p: "$P", o: "$O"}, c: { $sum: 1 }} }
,{ $group: { _id: "$_id.o" , c: { $sum: "$c" } }}
]);
You can probably read it and figure out yourself what's happening in that last step, but just in case I'll explain. the last step is group all documents that have the same organization id, and then summing the quantity specified by the previous c field. After the first group, you had two documents that both had a count c of 2 but different profile id. The next group ignores the profile id and just groups them if they have the same organization id and adds their counts.
When I ran this query, here is my result, which is what I think you're looking for:
{
"_id" : 10,
"c" : 4
}
Hope this helps. Let me know if you have any questions.