How to find count of a key in one document using mongoDB? - mongodb

I have following structure in my collection:
users:[
{
"name":"ABC",
"address":{
"city":"London",
"country":"UK",
}
},
{
"name":"XYZ",
"address":{
"city":"London",
"country":"UK",
}
},
{
"name":"PQR",
"address":{
"city":"NewYork",
"country":"US",
}
}
]
I want count of number of occurrences of 'city' key in 'address' and 'name' as a result.
I want to query above collection and want following output:
[{
"name":"ABC",
"city":"London",
"count":2
},{
"name":"XYZ",
"city":"London",
"count":2
}, {
"name":"PQR",
"city":"NewYork",
"count":1
}
]

I simulated your collection
{
"_id" : ObjectId("547c30ae371ea419f07b9550"),
"users" : [
{
"name" : "ABC",
"address" : {
"city" : "London",
"country" : "UK"
}
},
{
"name" : "XYZ",
"address" : {
"city" : "London",
"country" : "UK"
}
},
{
"name" : "PQR",
"address" : {
"city" : "NewYork",
"country" : "US"
}
}
]
}
And then I use aggregate framework
db.coll.aggregate([
{
$unwind:"$users"
},
{
$group:{
_id:"$users.address.city",
name:{$push:"$users.name"},
city:{$first:"$users.address.city"},
count:{$sum:1}
}
},{
$unwind:"$name"
},{
$project:{
_id:0,
"city":"$_id",
"name":1,
"city":1,
"count":1
}
}])
result:
{
"result" : [
{
"name" : "PQR",
"city" : "NewYork",
"count" : 1
},
{
"name" : "ABC",
"city" : "London",
"count" : 2
},
{
"name" : "XYZ",
"city" : "London",
"count" : 2
}
],
"ok" : 1
}
UPDATE AFTER QUESTION
I added a new Document
{
"_id" : ObjectId("547c394c371ea419f07b9551"),
"users" : [
{
"address" : {
"city" : "Livorno",
"country" : "LI"
}
},
{
"address" : {
"city" : "Livorno",
"country" : "LI"
}
},
{
"address" : {
"city" : "NewYork",
"country" : "US"
}
}
]
}
and new Query
db.coll.aggregate([
{
$unwind:"$users"
},
{
$group:{
_id:"$users.address.city",
"name": {
$push:{"$ifNull": ["$users.name","$_id"]}
},
city:{$first:"$users.address.city"},
count:{$sum:1}
}
},{
$unwind:"$name"
},{
$project:{
_id:0,
"city":"$_id",
"name":1,
"city":1,
"count":1
}
}])
Result:
{
"result" : [
{
"name" : "PQR",
"city" : "NewYork",
"count" : 2
},
{
"name" : ObjectId("547c394c371ea419f07b9551"),
"city" : "NewYork",
"count" : 2
},
{
"name" : ObjectId("547c394c371ea419f07b9551"),
"city" : "Livorno",
"count" : 2
},
{
"name" : ObjectId("547c394c371ea419f07b9551"),
"city" : "Livorno",
"count" : 2
},
{
"name" : "ABC",
"city" : "London",
"count" : 2
},
{
"name" : "XYZ",
"city" : "London",
"count" : 2
}
],
"ok" : 1
}

Related

How to remove item in nested collection by id - mongoose

I want to remove a topic by id from the following collection. Here is the hierarchy of the collection - Area.
Area ---
|
---Subjects---
|
Courses---
|
Chapters----
|
Topics
The Area example is as following.
{
"_id" : ObjectId("607f7c67f64e993a5c9b9793"),
"name" : "Automatic Engineering",
"description" : "this is the description.",
"created" : ISODate("2021-04-21T01:14:15.736Z"),
"subjects" : [
{
"description" : "dddd",
"name" : "Transfer Function",
"_id" : ObjectId("607f7c7af64e993a5c9b9794"),
"created" : ISODate("2021-04-21T01:14:34.943Z"),
"courses" : [
{
"description" : "dddd",
"name" : "conception",
"_id" : ObjectId("607f7c9af64e993a5c9b9796"),
"created" : ISODate("2021-04-21T01:15:06.696Z"),
"chapters" : [
{
"description" : "chapter 1",
"name" : "Chapter 1",
"_id" : ObjectId("607f7cb4f64e993a5c9b9798"),
"created" : ISODate("2021-04-21T01:15:32.855Z"),
"topics" : [
{
"description" : "1",
"name" : "Topic 1",
"_id" : ObjectId("607f7ccef64e993a5c9b979b"),
"created" : ISODate("2021-04-21T01:15:58.064Z")
},
{
"description" : "2",
"name" : "Topic 2",
"_id" : ObjectId("607f7cd5f64e993a5c9b979c"),
"created" : ISODate("2021-04-21T01:16:05.663Z")
},
{
"description" : "3",
"name" : "Topic 3",
"_id" : ObjectId("607f7cdcf64e993a5c9b979d"),
"created" : ISODate("2021-04-21T01:16:12.336Z")
}
]
},
{
"description" : "chapter2",
"name" : "Chapter 2",
"_id" : ObjectId("607f7cbcf64e993a5c9b9799"),
"created" : ISODate("2021-04-21T01:15:40.231Z"),
"topics" : []
},
{
"description" : "chapter 3",
"name" : "Chapter 3",
"_id" : ObjectId("607f7cc5f64e993a5c9b979a"),
"created" : ISODate("2021-04-21T01:15:49.000Z"),
"topics" : []
}
]
},
{
"description" : "dddd",
"name" : "exercise",
"_id" : ObjectId("607f7ca6f64e993a5c9b9797"),
"created" : ISODate("2021-04-21T01:15:18.465Z"),
"chapters" : []
}
]
},
{
"description" : "this is the example",
"name" : "State Space Equation",
"_id" : ObjectId("607f7c8ef64e993a5c9b9795"),
"created" : ISODate("2021-04-21T01:14:54.056Z"),
"courses" : []
}
],
"__v" : 0
}
And I need to remove topic object in chapters array. So this is what I have done.
areaModel.findOneAndUpdate({
'_id': req.body.area_id,
'subjects._id': req.body.subject_id,
'courses._id': req.body.course_id,
'chapter._id': req.body.chapter_id
}, {
"$pull": {
"subjects.$.courses.$.chapters.$.topics": {
_id: req.body.topic_id
}
}
}, (err, result) =>{
if (err) {
return res.json({success: false, msg: err});
} else {
return res.json({success: true, msg:"successfully removed" });
}
});
By using $pull method of mongoose, I could remove the course from the area easily. But it was impossible for me to remove the more nested items like chapters and topics.
How can I solve this issue?
you can do so with the use of arrayFilters like so:
db.collection.updateOne(
{
_id: ObjectId("607f7c67f64e993a5c9b9793")
},
{
$pull: {
"subjects.$[s].courses.$[c].chapters.$[ch].topics": {
_id: ObjectId("607f7ccef64e993a5c9b979b")
}
}
},
{
arrayFilters: [
{
"s._id": { $eq: ObjectId("607f7c7af64e993a5c9b9794") }
},
{
"c._id": { $eq: ObjectId("607f7c9af64e993a5c9b9796") }
},
{
"ch._id": { $eq: ObjectId("607f7cb4f64e993a5c9b9798") }
}
]
})
https://mongoplayground.net/p/kVyV27nnkII
Try it:
areaModel.findOneAndUpdate(
{ _id: req.body.area_id },
{
$pull: {
"subjects.$[].courses.$[].chapters.$[].topics": {
_id: req.body.topic_id,
},
},
}
);

Spring Data Mongo - How to get the nested distinct array for nested value?

I'm taking a reference from : Spring Data Mongo - Perform Distinct, but doesn't wants to pull embedded documents in results and asking another questions.
I want to find technology list where "subdeptCd" : "1D". How can we do that ?
{
"firstName" : "Laxmi",
"lastName" : "Dekate",
.....
.......
.....
"departments" : {
"deptCd" : "Tax",
"deptName" : "Tax Handling Dept",
"status" : "A",
"subdepts" : [
{
"subdeptCd" : "1D",
"subdeptName" : "Tax Clearning",
"desc" : "",
"status" : "A"
"technology" : [
{
"technologyCd" : "4C",
"technologyName" : "Cloud Computing",
"desc" : "This is best certficate",
"status" : "A"
}
]
}
]
},
},
{
"firstName" : "Neha",
"lastName" : "Parate",
.....
.......
.....
"departments" : {
"deptCd" : "Tax Rebate",
"deptName" : "Tax Rebate Handling Dept",
"status" : "A",
"subdepts" : [
{
"subdeptCd" : "1D",
"subdeptName" : "Tax Clearning",
"desc" : "",
"status" : "A"
"technology" : [
{
"technologyCd" : "9C",
"technologyName" : "Spring Cloud",
"desc" : "This is best certficate post Google",
"status" : "A"
}
]
}
]
},
}
You can get distinct technologies (technology array elements) with this aggregation:
db.depts.aggregate( [
{
$unwind: "$departments.subdepts"
},
{
$unwind: "$departments.subdepts.technology"
},
{
$match: { "departments.subdepts.subdeptCd": "1D" }
},
{
$group: { _id: "$departments.subdepts.technology.technologyCd", tech: { $first: "$departments.subdepts.technology" } }
},
{
$replaceRoot: { newRoot: "$tech" }
}
] )

Problems aggregating MongoDB

I am having problems aggregating my Product Document in MongoDB.
My Product Document is:
{
"_id" : ObjectId("5d81171c2c69f45ef459e0af"),
"type" : "T-Shirt",
"name" : "Panda",
"description" : "Panda's are cool.",
"image" : ObjectId("5d81171c2c69f45ef459e0ad"),
"created_at" : ISODate("2019-09-17T18:25:48.026+01:00"),
"is_featured" : false,
"sizes" : [
"XS",
"S",
"M",
"L",
"XL"
],
"tags" : [ ],
"pricing" : {
"price" : 26,
"sale_price" : 8
},
"categories" : [
ObjectId("5d81171b2c69f45ef459e086"),
ObjectId("5d81171b2c69f45ef459e087")
],
"sku" : "5d81171c2c69f45ef459e0af"
},
And my Category Document is:
{
"_id" : ObjectId("5d81171b2c69f45ef459e087"),
"name" : "Art",
"description" : "These items are our artsy options.",
"created_at" : ISODate("2019-09-17T18:25:47.196+01:00")
},
My aim is to perform aggregation on the Product Document in order to count the number of items within each Category. So I have the Category "Art", I need to count the products are in the "Art" Category:
My current aggregate:
db.product.aggregate(
{ $unwind : "$categories" },
{
$group : {
"_id" : { "name" : "$name" },
"doc" : { $push : { "category" : "$categories" } },
}
},
{ $unwind : "$doc" },
{
$project : {
"_id" : 0,
"name" : "$name",
"category" : "$doc.category"
}
},
{
$group : {
"_id" : "$category",
"name": { "$first": "$name" },
"items_in_cat" : { $sum : 1 }
}
},
{ "$sort" : { "items_in_cat" : -1 } },
)
Which does actually work but not as I need:
{
"_id" : ObjectId("5d81171b2c69f45ef459e082"),
"name" : null, // Why is the name of the category no here?
"items_in_cat" : 4
},
As we can see the name is null. How can I aggregate the output to be:
{
"_id" : ObjectId("5d81171b2c69f45ef459e082"),
"name" : "Art",
"items_in_cat" : 4
},
We need to use $lookup to fetch the name from Category collection.
The following query can get us the expected output:
db.product.aggregate([
{
$unwind:"$categories"
},
{
$group:{
"_id":"$categories",
"items_in_cat":{
$sum:1
}
}
},
{
$lookup:{
"from":"category",
"let":{
"id":"$_id"
},
"pipeline":[
{
$match:{
$expr:{
$eq:["$_id","$$id"]
}
}
},
{
$project:{
"_id":0,
"name":1
}
}
],
"as":"categoryLookup"
}
},
{
$unwind:{
"path":"$categoryLookup",
"preserveNullAndEmptyArrays":true
}
},
{
$project:{
"_id":1,
"name":{
$ifNull:["$categoryLookup.name","NA"]
},
"items_in_cat":1
}
}
]).pretty()
Data set:
Collection: product
{
"_id" : ObjectId("5d81171c2c69f45ef459e0af"),
"type" : "T-Shirt",
"name" : "Panda",
"description" : "Panda's are cool.",
"image" : ObjectId("5d81171c2c69f45ef459e0ad"),
"created_at" : ISODate("2019-09-17T17:25:48.026Z"),
"is_featured" : false,
"sizes" : [
"XS",
"S",
"M",
"L",
"XL"
],
"tags" : [ ],
"pricing" : {
"price" : 26,
"sale_price" : 8
},
"categories" : [
ObjectId("5d81171b2c69f45ef459e086"),
ObjectId("5d81171b2c69f45ef459e087")
],
"sku" : "5d81171c2c69f45ef459e0af"
}
Collection: category
{
"_id" : ObjectId("5d81171b2c69f45ef459e086"),
"name" : "Art",
"description" : "These items are our artsy options.",
"created_at" : ISODate("2019-09-17T17:25:47.196Z")
}
{
"_id" : ObjectId("5d81171b2c69f45ef459e087"),
"name" : "Craft",
"description" : "These items are our artsy options.",
"created_at" : ISODate("2019-09-17T17:25:47.196Z")
}
Output:
{
"_id" : ObjectId("5d81171b2c69f45ef459e087"),
"items_in_cat" : 1,
"name" : "Craft"
}
{
"_id" : ObjectId("5d81171b2c69f45ef459e086"),
"items_in_cat" : 1,
"name" : "Art"
}

How to group nasted array in mongo db

In my mongodb collection I have next records
{ "_id" : ObjectId("5d0dfb68264b2d01a3237a3e"), "name" : "lexa", "cat" : 2, "gender" : "male", "date" : ISODate("2019-06-22T09:56:56.070Z") }
{ "_id" : ObjectId("5d0dfb6c264b2d01a3237a3f"), "name" : "dima", "cat" : 2, "gender" : "male", "date" : ISODate("2019-06-22T09:57:00.925Z") }
{ "_id" : ObjectId("5d0dfb75264b2d01a3237a40"), "name" : "lena", "cat" : 2, "gender" : "female", "date" : ISODate("2019-06-22T09:57:10.003Z") }
{ "_id" : ObjectId("5d0dfb7a264b2d01a3237a41"), "name" : "nina", "cat" : 2, "gender" : "female", "date" : ISODate("2019-06-22T09:57:14.941Z") }
{ "_id" : ObjectId("5d0dfb8f264b2d01a3237a42"), "name" : "nina", "cat" : 1, "gender" : "female", "date" : ISODate("2019-06-22T09:57:35.128Z") }
{ "_id" : ObjectId("5d0dfb93264b2d01a3237a43"), "name" : "lena", "cat" : 1, "gender" : "female", "date" : ISODate("2019-06-22T09:57:39.789Z") }
{ "_id" : ObjectId("5d0dfb9b264b2d01a3237a44"), "name" : "dima", "cat" : 1, "gender" : "male", "date" : ISODate("2019-06-22T09:57:47.150Z")
Then I use aggregation mongo framework to group that records by cat.
db.foo.aggregate([{'$group':
{
'_id': '$cat',
'users':
{'$push':
{
'name':'$name',
'gender': '$gender'
}
}
}
}])
That query returns me next result
{
"_id" : 1,
"users" : [
{
"name" : "nina",
"gender" : "female"
},
{
"name" : "lena",
"gender" : "female"
},
{
"name" : "dima",
"gender" : "male"
}
]
}
{
"_id" : 2,
"users" : [
{
"name" : "lexa",
"gender" : "male"
},
{
"name" : "dima",
"gender" : "male"
},
{
"name" : "lena",
"gender" : "female"
},
{
"name" : "nina",
"gender" : "female"
}
]
}
And the question is what I need to add to my query to group by my users array. I want to get something like this
{
"_id" : 1,
"users" : [
{
"gender": "male",
"names": ["dima"]
},
{
"gender": "female",
"names": ["lena", "nina"]
}
]
}
{
"_id" : 2,
"users" : [
{
"gender": "male",
"names": ["lexa", "dima"]
},
{
"gender": "female",
"names": ["lena", "nina"]
}
]
}
I need to have my nested array been grouped too without loosing first group result
Would be easier to first group by cat X gender and then restructure the data like so :
db.foo.aggregate([
{'$group':
{
'_id': {cat: '$cat', gender: "$gender"},
'names':
{'$push':
{
'name':'$name',
}
}
}
},
{
$group: {
'_id': "$_id.cat",
users: { $push: { gender: "$_id.gender", names: "$names" }
}
}
])

Mongodb aggregation - sum a grouped, addToSet field

I've got the following query:
db.listener.aggregate(
[
{ "$match" : { "location.countryName" : "Italy" } },
{ "$project" : { "location" : "$location"} },
{ "$group" : { "_id" : { "country": "$location.countryName", "city": "$location.cityName" }, "count" : { "$sum" : 1 }, "co-ords" : { "$addToSet" : { "lat" : "$location.latitude", "long" : "$location.longitude" } } } },
{ "$group" : { "_id" : "$_id.country", "cities" : { "$push" : { "city" : "$_id.city", "count" : "$count", "co-ords" : "$co-ords" } } } },
{ "$sort" : { "_id" : 1 } },
]
)
which give the following results (truncated):
{
"result" : [
{
"_id" : "Italy",
"cities" : [
{
"city" : "Seriate",
"count" : 1,
"co-ords" : [
{
"lat" : "45.6833",
"long" : "9.7167"
}
]
},
{
"city" : "Milan",
"count" : 3,
"co-ords" : [
{
"lat" : "45.4612",
"long" : "9.1878"
},
{
"lat" : "45.4667",
"long" : "9.2"
}
]
},
As you can see in the example for the city of Milan, the total city count is 3 but the number of longitude/latitude sets is two. This means that one of those more precise locations has two instances and the other has one.
I now need to amend my query to reflect the number of instances per latitude/longitude as well as the overall count.
It might look something like this:
{
"city" : "Milan",
"count" : 3,
"co-ords" : [
{
"lat" : "45.4612",
"long" : "9.1878",
"total" : 2
},
{
"lat" : "45.4667",
"long" : "9.2",
"total" : 1
}
]
},
I've tried a few things to achieve this but it never come out as I'd like or Mongo throws an error.
Anyone know the best way to do this?
Many thanks,
Nick.
db.listener.aggregate(
[
{ "$match" : { "location.countryName" : "Italy" } },
{ "$group" : { "_id" : { "country": "$location.countryName",
"city": "$location.cityName",
"coords": { "lat" : "$location.latitude", "long" : "$location.longitude" }
},
"count" : { "$sum" : 1 }
}
},
{ "$group" : { "_id" : { "country": "$_id.country", "city": "$_id.city" },
"coords": { "$addToSet" : { "long" : "$_id.coords.long",
"lat" : "$_id.coords.lat",
"total" : "$count"
}
},
"count" : { "$sum" : "$count" }
}
},
{ "$group" : { "_id" : "$_id.country",
"cities" : { "$push" : { "city" : "$_id.city",
"count" : "$count",
"coords" : "$coords" } } } },
{ "$sort" : { "_id" : 1 } },
]);
Sample output on your data from this:
{ "_id" : "Italy",
"cities" : [
{
"city" : "Seriate",
"count" : 1,
"coords" : [
{
"long" : "9.7167",
"lat" : "45.6833",
"total" : 1
}
]
},
{
"city" : "Milan",
"count" : 3,
"coords" : [
{
"long" : "9.1878",
"lat" : "45.4612",
"total" : 1
},
{
"long" : "9.2",
"lat" : "45.4667",
"total" : 2
}
]
}
]
}