can we sort result of group() in mongoDB? - mongodb

I have one scenario like need to sort the result of group() in mongoDB
For Example:
Suppose you have documents in Student collection like below
{
"id":"kjdkfdjkljflkd73847",
"DepartementID":123,
"parties":[
{
"idNumber" : "103",
"studentName" : {
"suffix" : "Mr",
"firstName" : "ram",
"middleName" : "",
"lastName" : "gorli"
}
},
{
"idNumber" : "99",
"studentName" : {
"suffix" : "Mr",
"firstName" : "ramesh",
"middleName" : "",
"lastName" : "vogue"
}
},
]
},
{
"id":"kjdkfdjkljflkd73847",
"DepartementID":123,
"parties":[
{
"idNumber" : "101",
"studentName" : {
"suffix" : "Mr",
"firstName" : "Mike",
"middleName" : "",
"lastName" : "john"
}
},
{
"idNumber" : "102",
"studentName" : {
"suffix" : "Mr",
"firstName" : "ram",
"middleName" : " ",
"lastName" : "gorli"
}
},
]
}
I'm grouping the details by using DepartmentID in student collection, In group() itself I need to sort by using last name in parties. how can we achieve this in one query it self?
please help me out. I really appreciate your efforts.

This will work for you :
db.getCollection('quotes').aggregate([
{
$unwind:"$parties"
},
{
$sort:{
"parties.studentName.lastName": 1
}
},
{
$group:{
_id:"$DepartementID",
parties:{
$push:"$parties"
}
}
}
])

Related

How do I remove duplicates in mongodb based on multiple array fields?

Hi this is an example of my structure.
"_id" : ObjectId("12312341234cf123cc123456"),
"internetMessageId" : "<1234#1234asdfasc.COM>",
"ccRecipients" : [
{
"emailAddress" : {
"name" : "Joe"
"address" : "joe#gmail.com"
}
},
{
"emailAddress" : {
"name" : "Marc",
"address" : "marc#gmail.com"
}
}
],
"toRecipients" : [
{
"emailAddress" : {
"name" : "Mary"
"address" : "mary#gmail.com"
}
},
{
"emailAddress" : {
"name" : "Frank",
"address" : "frank#gmail.com"
}
}
],
"bccRecipients" : [],
"from" : {
"emailAddress" : {
"name" : "Juan",
"address" : "juan#gmail.com"
}
}
}
I have several duplicated documents as above and I want to keep only one of them. I understand that I should create a unique combination of columns from, internetMessadeId, toRecipients, ccRecipients, and bccRecipients but I'm struggling to put it all together.
Thanks for any help.

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" }
}
] )

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 JsonObject and JsonArray

I'm new MongoDB.
I have a collection is named "surveyCollection".
I insert some data to collection with shell,
db.surveyCollection.insert({
"count":30,
"people": [
{
"firstName":"John",
"lastName":"Jason"
}
]
})
db.surveyCollection.insert({
"count":30,
"people": [
{
"firstName":"Sarah",
"lastName":"Smith"
}
]
})
And I have data below ( db.surveyCollection.find().pretty());
{
"_id" : ObjectId("5873d0c86c3319a579754d00"),
"count" : 30,
"people" : [
{
"firstName" : "John",
"lastName" : "Jason"
}
]
}
{
"_id" : ObjectId("5873d0c86c3319a579754d00"),
"count" : 30,
"people" : [
{
"firstName" : "Sarah",
"lastName" : "Smith"
}
]
}
I don't know how can I insert a new people to any survey. I mean, I want to have datas like below;
{
"_id" : ObjectId("5873d0c86c3319a579754d00"),
"count" : 30,
"people" : [
{
"firstName" : "John",
"lastName" : "Jason"
}
]
}
{
"_id" : ObjectId("5873d0c86c3319a579754d00"),
"count" : 30,
"people" : [
{
"firstName" : "Sarah",
"lastName" : "Smith"
},
{
"firstName" : "George",
"lastName" : "Smith"
}
]
}
I mean, how can I reach values in JsonArray?
Thank you
Use $push method to push an item into array of a document as shown below :
db.surveyCollection.update(
{ "_id" : ObjectId("5873d0c86c3319a579754d00") },
{ $push: { people: { "firstName" : "George", "lastName" : "Smith"} } }
)
This will add the object { "firstName" : "George", "lastName" : "Smith"} to the array people in the document of {"_id" : ObjectId("5873d0c86c3319a579754d00")}

MongoDB find substructure

I have a mongodb with a table named Patient. When I display the content with MongoVUE I see my Patients in this format:
/* 0 */
{
"_id" : ObjectId("547c4aa9dbe9665042dddf76"),
"Patient" : {
"Maidenname" : { },
"Phone" : {
"Type" : { },
"Number" : { }
},
"Citizenship" : { },
"SSN" : 1234567,
"Profession" : { },
"systemUID" : { },
"lid" : 111,
"system" : "abc",
"Address" : {
"Street" : { },
"State" : { },
"Zip" : { },
"Country" : { },
"City" : { }
},
"Lastname" : "asdf",
"Firstname" : "Test",
"Birthdate" : 19000101,
"Identifier" : {
"id" : 123,
"system" : "abc",
"UID" : { }
}
}
}
I would like to make a find on the field Firstname with value Test, this is my query:
db.Patient.find({Firstname:"Test"})
But it returns 0 rows.
I also tried this one:
db.Patient.find({Patient : {Firstname:"Test"}})
Also 0 rows returned.
When I do a find like this:
db.Patient.find()
I get all data. (also the one with "Firstname" : "Test")
Can anyone help me with that find query?
Should try this it work well
db.patiens.find({"Patient.Firstname":"Test"})
Since Firstname is in Patient object, it is its property you need to select is as
db.Patient.find({"Patient.Firstname":"Test"})