MongoDB - sum specific array element under conditions exclude duplicate - mongodb

I have a bunch of docs that look like below:
{
"_id" : ObjectId("8f30b453c2ece001364dc04d"),
"SessionId" : "awkuTQjj53kgqAZ4J",
"StartDate" : ISODate("2020-02-24T11:51:36.918+0000"),
"EndDate" : ISODate("2020-02-24T11:51:36.918+0000"),
"List1" : "X",
"List2" : "Y",
"rating" : [
{
"ObjectId" : "5d09e98380c5d5eb89ac5069",
"List" : "List 2",
"Rate" : NumberInt(5),
"RatedDate" : ISODate("2020-02-24T11:55:47.774+0000")
},
{
"ObjectId" : "5d09e98380c5d5eb89ac5069",
"List" : "List 2",
"Rate" : NumberInt(4),
"RatedDate" : ISODate("2020-02-24T11:55:48.408+0000")
},
{
"ObjectId" : "5d09e98380c5d5eb89ac505b",
"List" : "List 2",
"Rate" : NumberInt(3),
"RatedDate" : ISODate("2020-02-24T11:55:49.520+0000")
},
{
"ObjectId" : "5d09e98380c5d5eb89ac505c",
"List" : "List 2",
"Rate" : NumberInt(3),
"RatedDate" : ISODate("2020-02-24T11:55:51.787+0000")
},
{
"ObjectId" : "5d09e98380c5d5eb89ac5057",
"List" : "List 1",
"Rate" : NumberInt(4),
"RatedDate" : ISODate("2020-02-24T11:55:53.865+0000")
},
{
"ObjectId" : "5d09e98380c5d5eb89ac5058",
"List" : "List 1",
"Rate" : NumberInt(4),
"RatedDate" : ISODate("2020-02-24T11:55:53.865+0000")
},
],
"Answers" : {
"SelectedList" : "1",
},
}
I need to sum up all the rating.Rate where rating.List:'List 1' and respectively sum up all rating.Rate where rating.List:'List 2', also exclude duplicate records (by rating.ObjectId) and count only the ones with latest rating.RatedDate. I suppose this is a group aggregation.
Also they should match the criteria
List1:'X' ,
Answers.selectedList:1
What I have written looks like below so far:
[
{
"$match" : {
"List1" : "X",
"Answers.SelectedList" : "1"
}
},
{
"$unwind" : {
"path" : "$rating"
}
},
{
"$group" : {
"_id" : null,
"sum" : {
"$sum" : "$Rate"
}
}
}
]
can you please help me?

I was a little confused around the List1/List2 however I think this will get you most of the way to your required aggregation query.
db.test.aggregate([
{
$match: {
"List1": "X",
"Answers.SelectedList": "1"
}
},
{
"$unwind" : "$rating"
},
{
$group:{
_id: {
id: "$rating.ObjectId",
list: "$rating.List"
},
maxRatedDate: { $max: "$rating.RatedDate" },
ratings: { $push: "$rating" }
}
},{
$addFields: {
ratings: {
$filter: {
input: "$ratings",
as: "item",
cond: { $eq: [ "$$item.RatedDate", "$maxRatedDate" ] }
}
}
}
},
{
$unwind: "$ratings"
},
{
$group:{
_id: "$ratings.List",
sum : {
$sum : "$ratings.Rate"
}
}
}
])
This will output the following
{ "_id" : "List 1", "sum" : 8 }
{ "_id" : "List 2", "sum" : 10 }
However, let's try to break it down.
To start with we've got a simple match, the same as yours in your question. this just limits the number of documents we pass back
$match: {
"List1": "X",
"Answers.SelectedList": "1"
}
Then we unwind all the array items so we get a document for each rating, this allows us to do some extra querying on the data.
{
"$unwind" : "$rating"
}
Next, we've got a group by, here we're a group on the ObjectId of the rating so we can later remove duplicates, we're also finding out in the group which rating we've group has the highest date so we can take that one later in a projection. we're then pushing all the rating back in the array for later.
$group:{
_id: {
id: "$rating.ObjectId",
list: "$rating.List"
},
maxRatedDate: { $max: "$rating.RatedDate" },
ratings: { $push: "$rating" }
}
Next we want to project the ratings array in to a single element in which it only contains the latest rating, for this we use a $filter on the array and filter them all out that don't match our max date we calculated in our previous step.
$addFields: {
ratings: {
$filter: {
input: "$ratings",
as: "item",
cond: { $eq: [ "$$item.RatedDate", "$maxRatedDate" ] }
}
}
}
The next two steps are fairly simple and are just unwinding the array again (we've only got one element, then grouping them to get the total sum for the lists.
{
$unwind: "$ratings"
},
{
$group:{
_id: "$ratings.List",
sum : {
$sum : "$ratings.Rate"
}
}
}

At this point you only need to provide the $group stage with the field that you're actually grouping on as the _id field and reference the fields properly as they are still inside of the rating array:
"$group" : {
"_id" : "$rating.List",
"sum" : {
"$sum" : "$rating.Rate"
}
}

Related

How to get the recent values of grouped result?

Below is the document which has an array name datum and I want to filter the records based on StatusCode, group by Year and sum the amount value from the recent record of distinct Types.
{
"_id" : ObjectId("5fce46ca6ac9808276dfeb8c"),
"year" : 2018,
"datum" : [
{
"StatusCode" : "A",
"Type" : "1",
"Amount" : NumberDecimal("100"),
"Date" : ISODate("2018-05-30T00:46:12.784Z")
},
{
"StatusCode" : "A",
"Type" : "1",
"Amount" : NumberDecimal("300"),
"Date" : ISODate("2023-05-30T00:46:12.784Z")
},
{
"StatusCode" : "A",
"Type" : "2",
"Amount" : NumberDecimal("420"),
"Date" : ISODate("2032-05-30T00:46:12.784Z")
},
{
"StatusCode" : "B",
"Type" : "2",
"Amount" : NumberDecimal("420"),
"Date" : ISODate("2032-05-30T00:46:12.784Z")
}
]
}
In my case following is the expected result :
{
Total : 720
}
I want to achieve the result in the following aggregate Query pattern
db.collection.aggregate([
{
$addFields: {
datum: {
$reduce: {
input: "$datum",
initialValue: {},
"in": {
$cond: [
{
$and: [
{ $in: ["$$this.StatusCode", ["A"]] }
]
},
"$$this",
"$$value"
]
}
}
}
}
},
{
$group: {
_id: "$year",
RecentValue: { $sum: "$datum.Amount" }
}
}
])
You can first $unwind the datum array. Do the filtering and sort by the date. Then get the record with latest datum by a $group. Finally do another $group to calculate the sum.
Here is a mongo playground for your reference.

MongoDB - Find document by _id and return without child elements by value

I'm working on a project where I'm trying to return a document, but exclude some child fields based on status. For example if the status is disabled then I don't want that child returned. But all the other records returned if they don't contain disabled.
The request includes the _id of the document that I want to find and return, without the 'disabled' child records.
How do I select the document by _id then, exclude records from the child array based on a value.
Thanks
My document look like this:
{
"_id" : ObjectId("5e7bb266071f9601b6ad8f4e"),
"name" : "Test Document",
"postcode" : "90210",
"colors" : [
{
"_id" : ObjectId("5e7d276a05674f0cf49bdcec"),
"color" : "blue",
"status": "active"
},
{
"_id" : ObjectId("5e7d276a05674f0cf49bdceg"),
"color" : "red",
"status": "active"
},
{
"_id" : ObjectId("5e7d276a05674f0cf49bdceh"),
"color" : "green",
"status" : "disabled"
}
]
}
How do I return:
{
"_id" : ObjectId("5e7bb266071f9601b6ad8f4e"),
"name" : "Test Document",
"postcode" : "90210",
"colors" : [
{
"_id" : ObjectId("5e7d276a05674f0cf49bdcec"),
"color" : "blue",
"status": "active"
},
{
"_id" : ObjectId("5e7d276a05674f0cf49bdceg"),
"color" : "red",
"status": "active"
}
]
}
I have been trying variations of:
findr.aggregate([
{
$match: {
$and: [{
_id: mongodb.ObjectId(_id)
}, {
'color.status': 'active'
}]
}
},
{
$project: {
_id
name: 1,
postcode: 1,
colors: {
$filter: {
input: '$colors',
as: 'color',
cond: {
$eq: ['$$color.status', 'active']
}
}
}
}
}
])
Here is the code for filteration.
db.collection.aggregate([
{
$match: {
"_id": ObjectId("5e7bb266071f9601b6ad8f4e")
}
},
{
$project: {
items: {
$filter: {
input: "$colors",
as: "item",
cond: {
$eq: [
"$$item.status",
"active"
]
}
}
}
}
}
])
Playground

How to filter Mongodb $lookup results to get only the matched nested objects?

I have a customers collection such as;
{
"_id" : ObjectId("5de8c07dc035532b489b2e23"),
"name" : "sam",
"orders" : [{"ordername" : "cola"},{"ordername" : "cheesecake"}]
}
And waiters collection such as;
{
"_id" : ObjectId("5de8bc24c035532b489b2e20"),
"waiter" : "jack",
"products" : [{"name" : "cola", "price" : "4"},
{"name" : "water", "price" : "2"},
{"name" : "coffee", "price" : "8" }]
}
{
"_id" : ObjectId("5de8bdc7c035532b489b2e21"),
"waiter" : "susan",
"products" : [{"name" : "cheesecake", "price" : "12" },
{"name" : "apple pie", "price" : "14" }]
}
I want to join the objects from waiters collection into the customers collection by matching "products.name" and "orders.ordername". But, the result includes the whole document from the waiters collection, however, I want only the matched objects inside the document. Here is what I want;
ordered:[
{"name" : "cola", "price" : "4"},
{"name" : "cheesecake", "price" : "12" },
]
I tried $lookup with and without pipeline, and filter but could not get this result. Thanks in advance.
You had the right idea, we just have to "massage" the data a bit due to its structure like so:
db.collection.aggregate([
{
$addFields: {
"orderNames":
{
$reduce: {
input: "$orders",
initialValue: [],
in: {$concatArrays: [["$$this.ordername"], "$$value"]}
}
}
}
},
{
$lookup:
{
from: "waiters",
let: {orders: "$orderNames"},
pipeline: [
{
$unwind: "$products"
},
{
$match:
{
$expr:{$in: ["$products.name", "$$orders"]},
}
},
{
$group: {
_id: "$products.name",
price: {$first: "$products.price"}
}
},
{
$project: {
_id: 0,
price: 1,
name: "$_id"
}
}
],
as: "ordered"
}
}
])
It feels like you could benefit from a new collection of mapping items to prices. Could potentially save you a lot of time.

MongoDB: projection $ when find document into nested arrays

I have the following document of collection "user" than contains two nested arrays:
{
"person" : {
"personId" : 78,
"firstName" : "Mario",
"surname1" : "LOPEZ",
"surname2" : "SEGOVIA"
},
"accounts" : [
{
"accountId" : 42,
"accountRegisterDate" : "2018-01-04",
"banks" : [
{
"bankId" : 1,
"name" : "Bank LTD",
},
{
"bankId" : 2,
"name" : "Bank 2 Corp",
}
]
},
{
"accountId" : 43,
"accountRegisterDate" : "2018-01-04",
"banks" : [
{
"bankId" : 3,
"name" : "Another Bank",
},
{
"bankId" : 4,
"name" : "BCT bank",
}
]
}
]
}
I'm trying to get a query that will find this document and get only this subdocument at output:
{
"bankId" : 3,
"name" : "Another Bank",
}
I'm getting really stucked. If I run this query:
{ "accounts.banks.bankId": "3" }
Gets the whole document. And I've trying combinations of projection with no success:
{"accounts.banks.0.$": 1} //returns two elements of array "banks"
{"accounts.banks.0": 1} //empty bank array
Maybe that's not the way to query for this and I'm going in bad direction.
Can you please help me?
You can try following solution:
db.user.aggregate([
{ $unwind: "$accounts" },
{ $match: { "accounts.banks.bankId": 3 } },
{
$project: {
items: {
$filter: {
input: "$accounts.banks",
as: "bank",
cond: { $eq: [ "$$bank.bankId", 3 ] }
}
}
}
},
{
$replaceRoot : {
newRoot: { $arrayElemAt: [ "$items", 0 ] }
}
}
])
To be able to filter accounts by bankId you need to $unwind them. Then you can match accounts to the one having bankId equal to 3. Since banks is another nested array, you can filter it using $filter operator. This will give you one element nested in items array. To get rid of the nesting you can use $replaceRoot with $arrayElemAt.

Closed: I want to return list of ids in my aggregate query, i am new to mongodb

I am using below aggregate query to get the list of restaurant matches keyword search "chinese" within the list of ids passed,
db.business.aggregate([
{
$match:{
$text:{
$search:"chinese"
}
}
},
{
$match:{
"_id":{
$in:[
ObjectId("571453a82ece1392240f7b91"),
ObjectId("5714537b2ece1392240f7b8c"),
ObjectId("5714539a2ece1392240f7b8e"),
ObjectId("571453962ece1392240f7b8d")
]
}
}
},
])
Below is the sample data in mongodb.
{
"_id" : ObjectId("571453b32ece1392240f7b93"),
"_class" : "com.halal.sa.data.entities.Business",
"name" : "Chillies",
"description" : "nice restaurant",
"cuisine" : [
"veg-nonveg",
"chinese",
"kabab"
],
"address" : {
"streetAddress" : "1000 bentley road",
"city" : "marietta",
"pincode" : 30067,
"landmark" : "near delk road",
"location" : {
"type" : "Point",
"coordinates" : [
-84.4774305,
33.9202151
]
}
},
"phone" : 123,
"email" : "my#email.com",
"ownerEmail" : "test#email",
"status" : "2",
"website" : "test.com",
"authenticity" : "1"
}
please let me know the exact modified aggregate query which will only return list of _ids instead of returning all the documents from the collection. Thanks in advance
The way I see to have really only an array of ids (like you do with distinct in find), would be by using $group
db.business.aggregate([
{
$match:{
$text:{
$search:"chinese"
}
}
},
{
$match:{
"_id": {
$in: [
ObjectId("571453a82ece1392240f7b91"),
ObjectId("5714537b2ece1392240f7b8c"),
ObjectId("5714539a2ece1392240f7b8e"),
ObjectId("571453962ece1392240f7b8d")
]
}
}
},
{ $group: { _id: null, ids: { $push: "$$ROOT._id" } } }
]);
The result would give you basically something like
[
{
_id: null,
ids: [/* Your ids */],
}
]
I got it just used the $project in the query, thats it :-)
db.business.aggregate([
{ $match: { $text: { $search: "chinese" } } },
{ $match: { "_id": {$in : [ObjectId("571453a82ece1392240f7b91"), ObjectId("5714537b2ece1392240f7b8c"),ObjectId("5714539a2ece1392240f7b8e"),ObjectId("571453962ece1392240f7b8d")]} } },
{$project: {"_id": "$_id"}}
])