Count repeated objets in document in mongo - mongodb

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
}

Related

MongoDB needs to get total counts from all objects

my aggregate query looks like this. I need to get the total count from the value.
db.getCollection('mydesk').aggregate([
{
$match: {
"accountId": ObjectId("616ea615edc5fa4278ccb7f6"),
"val" : { $ne : null},
"deskId": { "$in": [
ObjectId("61934f7efdb9dc5a7c1c3a01"),
ObjectId("61713730857c3243ec1d257c"),
ObjectId("629d9548e0c93e34e435e7b9"),
ObjectId("616eaf613bcd9655b8035a25"),
]}
}
},
{
$project: {
item: 1,
value: { $size: "$val.shapes" },
}
}
])
I got result like this. But need to get the total counts of my value.
/* 1 */
{
"_id" : ObjectId("616fab4f12b90d59d03f380e"),
"value" : 11
}
/* 2 */
{
"_id" : ObjectId("616fbad35700980a041cd190"),
"value" : 4
}
/* 3 */
{
"_id" : ObjectId("61713752857c3243ec1d257e"),
"value" : 12
}
Needed result :
{
"totalValueCount" : 27
}
Thanks in advance
One option is to use $group to $sum up the values:
db.getCollection('mydesk').aggregate([
{
$match: {
"accountId": ObjectId("616ea615edc5fa4278ccb7f6"),
"val" : { $ne : null},
"deskId": { "$in": [
ObjectId("61934f7efdb9dc5a7c1c3a01"),
ObjectId("61713730857c3243ec1d257c"),
ObjectId("629d9548e0c93e34e435e7b9"),
ObjectId("616eaf613bcd9655b8035a25"),
]}
}
},
{
$group: {
_id: null,
total: {$sum: { $size: "$val.shapes"}},
}
},
{$project: {_id: 0, total: 1}}
])

Count if a value exists inside the array of objects [ MongoDB ]

I am developing a simple chat application with MongoDB and got stuck into a situation.
My document in database is as
{
"_id" : ObjectId("605a217ed8168f4c262f4782"),
"message" : "Hi, This is a test message",
"created" : ISODate("2021-03-23T17:12:30.000Z"),
"user" : {
"_id" : ObjectId("5977af7df1d8cc4623283b14"),
"name" : "Sender Of Message"
},
"recipients" : [
{
"_id" : ObjectId("5977af7df1d8cc4623283b14"),
"time" : ISODate("2021-03-23T17:12:30.000Z")
},
{
"_id" : ObjectId("5df50a5eaa0e3c3104006101"),
"time" : ISODate("2021-03-23T17:12:35.000Z")
}
],
"target" : {
"_id" : ObjectId("5df50a5eaa0e3c3104006101"),
"name" : "Target Person"
},
"status" : 1
}
When I try to get the last message with the unread count of the user I am always getting 1
Here is the query that I tried on.
db.collection.aggregate([
{ $match: { 'target._id': ObjectId('5df50a5eaa0e3c3104006101'), status: 1 } },
{ $sort: { _id: -1 } },
{
$group: {
_id: '$user._id',
doc: { $first: '$$ROOT' },
unread: {
$sum: {
$cond: {
if: { $ne: [ ObjectId('5df50a5eaa0e3c3104006101'), '$recipients._id' ] },
then: 1,
else: 0
}
}
}
}
}
])
If the collection contains even just the one document above, it is supposed to give 0 as the object inside the recipients array already contains the _id as ObjectId('5df50a5eaa0e3c3104006101'), but I'm getting 1 for the unread count. Any help?
Here is the output that I get from the query
{
"_id" : ObjectId("5977af7df1d8cc4623283b14"),
"doc" : {
"_id" : ObjectId("605a217ed8168f4c262f4782"),
"message" : "Hi, This is a test message",
"created" : ISODate("2021-03-23T17:12:30.000Z"),
"user" : {
"_id" : ObjectId("5977af7df1d8cc4623283b14"),
"name" : "Sender Of Message"
},
"recipients" : [
{
"_id" : ObjectId("5977af7df1d8cc4623283b14"),
"time" : ISODate("2021-03-23T17:12:30.000Z")
},
{
"_id" : ObjectId("5df50a5eaa0e3c3104006101"),
"time" : ISODate("2021-03-23T17:12:35.000Z")
}
],
"target" : {
"_id" : ObjectId("5df50a5eaa0e3c3104006101"),
"name" : "Target Person"
},
"status" : 1
},
"unread" : 1.0
}
I know why its showing with the count as 1
The array recipients contains an object with _id as ObjectId("5977af7df1d8cc4623283b14") inside it, so its a non matching condition. Which is causing the if condition to be satisfied and produce a value 1.
But I need to figure out how to query it to get the actual value.
Please note that I cant use $push operator on recipients array as it might have greater amount of object ( maybe in future )
Thanks for the support, but I have found the answer by myself.
Here is my approch to get the data as per the requirement.
Instead of searching for the records within the array what I did is
Filtered the data array to the _id that I don't need, so the array will have exactly one document or else it will be empty.
When taking the negation of the condition. ie, when there is one value in the array I need the counter to be 0 or else it should be 1
So I used the $size to check the array's size and $filter to filter out the other _ids and then used $sum to increment the counter as required.
db.collection.aggregate([
{ $match: { 'target._id': ObjectId('5df50a5eaa0e3c3104006101'), status: 1 } },
{ $sort: { _id: -1 } },
{
$group: {
_id: '$user._id',
doc: { $first: '$$ROOT' },
unread: {
$sum: {
$cond:{
if: {
$size: {
$filter: {
input: '$recipients',
as: 'item',
cond: { $eq: [ ObjectId('5df50a5eaa0e3c3104006101'), '$$item._id' ] }
}
}
},
then: 0,
else: 1
}
}
}
}
}
])
Try to Use like this:
db.getCollection('test').aggregate([
{ $match: { 'target._id': ObjectId('5df50a5eaa0e3c3104006101'), status: 1
} },
{ $sort: { _id: -1 } },
{ $unwind: { path: "$recipients", preserveNullAndEmptyArrays: true } },
{
$group: {
_id: '$user._id',
doc: { $first: '$$ROOT' },
unread: {
$sum: {
$cond: {
if: { $ne: ['$recipients._id',
ObjectId('5df50a5eaa0e3c3104006101') ] },
then: 1,
else: 0
}
}
}
}
}
])

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-

How to put the Mongodb results into an array?

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.

MongoDB order by a sum on a subset

I have the following collection:
error_reports
[
{
"_id":{
"$oid":"5184de1261"
},
"date":"29/04/2013",
"errors":[
{
"_id":"10",
"failures":2,
"alerts":1,
},
{
"_id":"11",
"failures":7,
"alerts":4,
}
]
},
{
"_id":{
"$oid":"5184de1262"
},
"date":"30/04/2013",
"errors":[
{
"_id":"15",
"failures":3,
"alerts":2,
},
{
"_id":"16",
"failures":9,
"alerts":1,
}
]
}
]
Is it possible to retrieve the list of documents with failures and alerts sum sorted by failures in descending order? I am new to mongodb, I have been searching for 2 days but I can't figure out what is the proper query...
I tried something like this :
db.error_reports.aggregate(
{ $sort : { failures: -1} },
{ $group:
{ _id: "$_id",
failures: { "$sum": "$errors.failures" }
}
}
);
But it didn't work, I think it is because of the $sum: $errors.failures thing, I would like to sum this attribute on every item of the day_hours subcollection but I don't know of to do this in a query...
You were very close with your attempt. The only thing missing is the $unwind aggregation operator. $unwind basically splits each document out based on a sub-document. So before you group the failures and alerts, you unwind the errors, like so:
db.error_reports.aggregate(
{ $unwind : '$errors' },
{ $group : {
_id : '$_id',
'failures' : { $sum : '$errors.failures' },
'alerts' : { $sum : '$errors.alerts' }
} },
{ $sort : { 'failures': -1 } }
);
Which gives you the follow result:
{
"result" : [
{
"_id" : ObjectId("5184de1262"),
"failures" : 12,
"alerts" : 3
},
{
"_id" : ObjectId("5184de1261"),
"failures" : 9,
"alerts" : 5
}
],
"ok" : 1
}