Get Distinct Document by Max Value of a Field - mongodb

I have a requirement where i should query on two fields out of which one is unique field and one is maximum field.
Here is my sample collection
{
"_id": ObjectId('59537b7fe08062b9ee8dfdf6'),
"admin": {
"model": "abc",
"version": "00",
"name":"john",
"age":"30"
}
}
{
"_id": ObjectId('59537b7fe08062b9ee8dfdf7'),
"admin": {
"model": "abc",
"version": "01" ,
"name":"john",
"age":"30"
}
}
{
"_id": ObjectId('59537b7fe08062b9ee8dfdf8'),
"admin": {
"model": "def",
"version": "00" ,
"name":"cena",
"age":"30"
}
}
I have two same models with different versions.I want to query for model with maximum version. I tried by simply sorting the version it does not work for me.
I am expecting output like this
{
"_id": ObjectId('59537b7fe08062b9ee8dfdf7'),
"admin": {
"model": "abc",
"version": "01" ,
"name":"john",
"age":"30"
}
}
{
"_id": ObjectId('59537b7fe08062b9ee8dfdf8'),
"admin": {
"model": "def",
"version": "00" ,
"name":"cena",
"age":"30"
}
}
Any suggestions will be really helpful.

As Neil said, it is $sort, $group, and $replaceRoot, but with correct values in the query:
db.collection.aggregate([
{ "$sort": { "admin.version": -1 } },
{ "$group": {
"_id": "$admin.model" ,
"admin": { "$first": "$$ROOT" }
}},
{ "$replaceRoot": { "newRoot": "$admin" } }
])

Related

How to use an existing field value with $push in MongoDb?

I have the following Mongo collection
{
"_id": ObjectId("5524d12d2702a21830bdb8e5"),
"code": "Apple",
"name": "iPhone",
"parameters": [
{
"code": "xxx",
"name": "Andrew",
"value": "9",
},
{
"code": "yyy",
"name": "Joy",
"value": "7",
},
]
}
I am using the following query to push into the parameters array object
db.coll.update({
"parameters.name": "Andrew"
},
{
$push: {
"parameters": {
"code": "$code",
"name": "bar",
"value": "10",
}
}
},
{
multi: true
})
However, for the value of code, I want to use the value of the object that matched (i.e. the object with parameters.name == "Andrew", which here is xxx.
Here's a playground link to the problem https://mongoplayground.net/p/v-j1tCCjiWq
Also, I am using a really old version (3.2) of MongoDb. It would be preferable if the solution worked with that.
With MongoDB v4.4+, you can first $match with your criteria. Chain up $first with $filter to extract the array element you want. Use $concatArrays to append a new element(i.e. same as $push) to the array and $merge to update back into the collection.
db.coll.aggregate([
{
$match: {
"parameters.name": "Andrew"
}
},
{
$set: {
parameters: {
"$concatArrays": [
"$parameters",
[
{
"$mergeObjects": [
// get the object matched
{
"$first": {
"$filter": {
"input": "$parameters",
"as": "p",
"cond": {
$eq: [
"Andrew",
"$$p.name"
]
}
}
}
},
// update the object matched with other fields with constant value
{
"name": "bar",
"value": "10"
}
]
}
]
]
}
}
},
{
"$merge": {
"into": "coll1",
"on": "_id"
}
}
])
Mongo Playground

MongoDb : Find and filter values from nested map

I have some date in mongo db
[
{
"_id": ObjectId("5a934e000102030405000000"),
"orgId": "606abce197dc265ac41ae82c",
"registrations": {
"id1": {
"status": "status",
"topStage": {
"id": "stage1",
"name": "stage1"
}
},
"id2": {
"status": "status",
"topStage": {
"id": "stage1",
"name": "stage1"
}
},
"id3": {
"status": "status",
"topStage": {
"id": "stage2",
"name": "stage2"
}
}
}
}
]
I am expecting to pass a stage id (at path registrations-> topStage -> id) and return all matching key values.
i have written following query
db.collection.aggregate([
{
$project: {
teams: {
$objectToArray: "$registrations"
},
original: "$$ROOT"
}
},
{
"$project": {
"teams": {
"$filter": {
"input": "$teams",
"as": "team",
"cond": {
"$eq": [
"$$team.v.topStage.id",
"stage1"
]
}
}
}
}
},
{
"$project": {
"registrations": {
"$arrayToObject": "$teams"
}
}
}
])
It does return me right values
for stage1 as stage id
[
{
"_id": ObjectId("5a934e000102030405000000"),
"registrations": {
"id1": {
"status": "status",
"topStage": {
"id": "stage1",
"name": "stage1"
}
},
"id2": {
"status": "status",
"topStage": {
"id": "stage1",
"name": "stage1"
}
}
}
}
]
and for stage2 as stage id, it returns
[
{
"_id": ObjectId("5a934e000102030405000000"),
"registrations": {
"id3": {
"status": "status",
"topStage": {
"id": "stage2",
"name": "stage2"
}
}
}
}
]
Can someone let me know if this is the best way to write this query or this can be simplified ??
It's the correct way to do it but there will be performance impact in the following cases.
If you don't have any other match condition against the indices
if you have a match condition and it matches few docs where registrations has more objects
Other best option you could do is that altering the schema.
you can keep registrations.id1 as registrations : { id:1, status_id: 2}
or you could alter the way such that it will not need to use objectToArray on larger set
if your data is huge, I would recommend to add an index on nested status Id field.
And mongo documentation itself suggests to evaluate multiple schemas for any data to get the best out of it.

Count the number of duplicate elements in MongoDB

I have the collection blow in mongodb:
{
"Id": "5",
"Group": [
{
"Name": "frank",
"Roll": "123"
}
]
},
{
"Id": "6",
"Group": [
{
"Name": "John",
"Roll": "124"
}
]
},
{
"Id": "7",
"Group": [
{
"Name": "John",
"Roll": "125"
}
]
}
The name "John" appears twice. I would like to display the number of each name that appears more than once:
{"Name": "John", "Count":2 }
You can use this aggregation query:
First $unwind to deconstruct the array and get all values as an object.
Then group by the name and $sum 1 for each name.
And then $match to get those values which exists more than one time (i.e. are repeated)
And last stage is to output values you want, in this case Name and Count.
db.collection.aggregate([
{
"$unwind": "$Group"
},
{
"$group": {
"_id": "$Group.Name",
"Count": {
"$sum": 1
},
}
},
{
"$match": {
"Count": {
"$gt": 1
}
}
},
{
"$project": {
"_id": 0,
"Name": "$_id",
"Count": 1
}
}
])
Example here

Sort records by array field values in MongoDb

I have a collection which has documents like;
{
"name": "Subject1",
"attributes": [{
"_id": "security_level1",
"level": {
"value": "100",
"valueKey": "ABC"
}
}, {
"_id": "security_score1",
"level": {
"value": "1000",
"valueKey": "CDE"
}
}
]
},
{
"name": "Subject2",
"attributes": [{
"_id": "security_level1",
"level": {
"value": "99",
"valueKey": "XYZ"
}
}, {
"_id": "security_score1",
"level": {
"value": "2000",
"valueKey": "EDF"
}
}
]
},
......
Each document will have so many attributes generated dynamically, can be different in size.
Is it possible to sort records based on level.value of security_level1? (security_level1 is _id field value)
As per above example, the second document ("name": "Subject2") should come first as the value ('level.value') of _id:security_level1 is 99, which is less than of Subject1's security_level1 value (100) - (Ascending order)
Use $filter and $arrayElemAt to get security_level1 item. Then you can use $toInt to convert that value to an integer so that $sort can be applied:
db.collection.aggregate([
{
$addFields: {
level: {
$let: {
vars: {
level_1: { $arrayElemAt: [ { $filter: { input: "$attributes", cond: { $eq: [ "$$this._id", "security_level1" ] } } } ,0] }
},
in: {
$toInt: "$$level_1.level.value"
}
}
}
}
},
{
$sort: {
level: 1
}
}
])
Mongo Playground

How to get distinct name and count in MongoDB using PyMongo

I have the below collection as shown below. All I want is the distinct "Name" and the count. For example Betty appears 2 times, so the output I want is Betty:2, Vic:1, Veronica:2. I am able to get the distinct Name by issuing the command "db.Car.find().distinct('Name')" but not sure how to get the count.
{
"Name": "Betty",
"Car": "Jeep",
}
{
"Name": "Betty",
"Car": "Van",
}
{
"Name": "Vic",
"Car": "Ferrari",
}
{
"Name": "Veronica",
"Car": "Bus",
}
{
"Name": "Veronica",
"Car": "Van",
}
You can just use $group to group by Name field and use $sum operator in it to get the Count field.
Something like below:
db.collection.aggregate([
{
"$group": {
"_id": "$Name",
"Count": {
"$sum": 1
}
}
},
{
"$project": {
"Name": "$_id",
"Count": 1,
"_id":0
}
}
])
The above will produce the following output:
[
{
"Count": 2,
"Name": "Betty"
},
{
"Count": 1,
"Name": "Vic"
},
{
"Count": 2,
"Name": "Veronica"
}
]