MongoDB get results where date is equal to max date - arrays

Assume I have the following document:
[
{
"callId": "17dac51e-125e-499e-9064-f20bd3b1a9d8",
"caller": {
"firstName": "Test",
"lastName": "Testing",
"phoneNumber": "1231231234"
},
"routeHistory": [
{
"assignedUserId": "cfa0ffe9-c77d-4eec-87d7-4430f7772e81",
"routeDate": "2020-01-01T06:00:00.000Z",
"status": "routed"
},
{
"assignedUserId": "cfa0ffe9-c77d-4eec-87d7-4430f7772e81",
"routeDate": "2020-01-03T06:00:00.000Z",
"status": "ended"
}
]
}
]
I want to get results where routeHistory.routeDate is equal to the $max routeDate value in routeHistory. I would expect my results to look like the following:
[
{
"callId": "17dac51e-125e-499e-9064-f20bd3b1a9d8",
"caller": {
"firstName": "Test",
"lastName": "Testing",
"phoneNumber": "1231231234"
},
"routeHistory": [
{
"assignedUserId": "cfa0ffe9-c77d-4eec-87d7-4430f7772e81",
"routeDate": "2020-01-03T06:00:00.000Z",
"status": "ended"
}
]
}
]
Is there a clean way to do this in a single aggregate, so that additional $match criteria can be applied?

You can use $let to define temporary variable being $max date and the use $filter along with $arrayElemAt to get first matching element:
db.collection.aggregate([
{
$addFields: {
routeHistory: {
$let: {
vars: {
maxDate: { $max: "$routeHistory.routeDate" }
},
in: {
$arrayElemAt: [
{ $filter: { input: "$routeHistory", cond: { $eq: [ "$$maxDate", "$$this.routeDate" ] } } },
0
]
}
}
}
}
}
])
Mongo Playground
EDIT:
version without $let:
db.collection.aggregate([
{
$addFields: {
maxDate: {
$max: "$routeHistory.routeDate"
}
}
},
{
$addFields: {
routeHistory: {
$arrayElemAt: [
{
$filter: { input: "$routeHistory", cond: { $eq: [ "$$maxDate", "$$this.routeDate" ] } }
},
0
]
}
}
}
}
])

Related

How to remove fields with empty values in objects nested in an array - mongodb

Need help with removing fields with empty values in array. So far, code is removing both values if one of the field is having empty value.
Example docment:
{
"_id": ObjectId("62ed3cfbeadf50344d622dd0"),
"Status": 1,
"AnswerList": [
{
"Question1": "some question1",
"Question2": "some question2",
"Answer": "",
"Comment": "Some comment"
},
{
"Question1": "some question1",
"Question2": "some question2",
"Answer": "some answer",
"Comment": ""
}
]
}
My query so far:
db.collection.aggregate([
{
$project: {
_id: 0,
Survey: 1,
Status: 1,
AnswerList: {
$map: {
input: "$AnswerList",
in: {
$cond: {
if: {
$in: [
"",
[
"$this.Comment",
"$this.Answer"
]
]
},
then: {
$arrayToObject: {
$filter: {
input: {
$map: {
input: {
$objectToArray: "$$this"
},
as: "element",
in: {
$cond: [
{
$in: [
"$$element.k",
[
"Comment",
"Answer"
]
]
},
null,
"$$element"
]
}
}
},
as: "filter",
cond: "$$filter"
}
}
},
else: "$$this"
}
}
}
}
}
}
])
One option is using $map and $filter with $arrayToObject and $objectToArray:
db.collection.aggregate([
{
$project: {
_id: 0,
Survey: 1,
Status: 1,
AnswerList: {
$map: {
input: "$AnswerList",
as: "item",
in: {$arrayToObject: {
$filter: {
input: {$objectToArray: "$$item"},
as: "pair",
cond: {$ne: ["$$pair.v", ""]}
}
}
}
}
}
}
}
])
See how it works on the playground example

Remove 3 similar items from an array using mongodb

I have this document and i need remove only 3 items with name "Test" using only 1 request.
{ "_id" : 1, "items" : ["Test, "Test", "Test", "Test", "Test", "Sword", "Sword]}
It must become { "_id" : 1, "items" : ["Test", "Test", "Sword", "Sword]} after request.
Maybe something like this:
db.collection.aggregate([
{
"$unwind": "$items"
},
{
$group: {
_id: "$items",
cnt: {
$sum: 1
},
it: {
$push: "$items"
},
origid: {
"$first": "$_id"
}
}
},
{
$project: {
_id: "$origid",
items: {
$slice: [
"$it",
0,
2
]
}
}
},
{
$group: {
_id: "$_id",
items: {
$push: "$items"
}
}
},
{
$addFields: {
items: {
"$reduce": {
"input": "$items",
"initialValue": [],
"in": {
"$concatArrays": [
"$$this",
"$$value"
]
}
}
}
}
}
])
Explained:
unwind the items array so you can group after
group by item so you get the repeating values
project with slice to remove more then 2x items in array
group to form back the document but without the >2 repeating values
project with concatArrays to concat arrays of items.
Playground
You can use this aggregation to get the desired result. The query uses the $reduce Aggregate Array Operator to iterate over the items array, and discards the first 3 matching "Test" items.
db.collection.aggregate([
{
$set: {
items: {
$reduce: {
input: "$items",
initialValue: { result: [], count: 0 },
in: {
$cond: [ { $and: [ { $eq: [ "$$this", "Test" ] }, { $lt: [ "$$value.count", 3 ] } ] },
{ result: "$$value.result", count: { $add: [ "$$value.count", 1 ] } },
{ result: { $concatArrays: [ "$$value.result", [ "$$this" ] ] }, count: "$$value.count" }
]
}
}
}
}
},
{
$set: {
items: "$items.result"
}
}
])

MongoDB - Match the key

I want to $match in a MongoDB, the number of documents in thousands, so looking for dynamic sol:
$doc.k equal to $info.data.k after k, $match then we have a document that contains only information that $matchs, see the expected output document. If you have any questions let me know.
[
{
"doc": {
"k": "ABC",
"v": {
"sec": 0
}
},
"info": [
{
"data": [
{
"k": "XYZ",
"v": {
"know": "alpha"
}
},
{
"k": "ABC",
"v": {
"know": "alpha"
}
}
]
}
]
}
]
The expected output document will look like this
[
{
"doc": {
"k": "ABC",
"v": {
"sec": 0
}
},
"info": [
{
"data": [
{
"k": "ABC",
"v": {
"know": "alpha"
}
}
]
}
]
}
]
$set - Update info field.
1.1. $map - As info is an array, need to iterate each document in the info array and return a new array.
1.1.1. $filter - Filter the document by doc.k and current iterate k value.
db.collection.aggregate([
{
$set: {
info: {
$map: {
input: "$info",
in: {
data: {
$filter: {
input: "$$this.data",
cond: {
$eq: [
"$doc.k",
"$$this.k"
]
}
}
}
}
}
}
}
}
])
Sample Mongo Playground

Mongodb use $elemMatch into $filter

I have a document like this:
I need to return the documents and filter the nested array (lessons) where any item of subLessons into input array
[{
"_id": {
"$oid": "6081fedbee5d133dbffb42eb"
},
"name": "my quiz",
"lessons": [
{
"_id": "460c42e1-b0b7-437e-ab63-c59cce8ced0d",
"name": "section",
"subLesson": [
{
"$oid": "6081fed9ee5d133dbffb3cba"
},
{
"$oid": "6081fed9ee5d133dbffb3cc0"
}
]
},
{
"_id": "f7b5c95f-1a68-42ca-880c-22ef3831ff03",
"name": "ffff",
"subLesson": [
{
"$oid": "6081fed9ee5d133dbffb3cbb"
}
]
}
]
}
}]
I wrote the following query but it does not work. I do not know how to use $elemMatch in $filter
db.collection.aggregate([
{
"$project": {
_id: 1,
lessons: {
$filter: {
"input": "$lessons",
"as": "lesson",
"cond": {
"$$lesson.subLesson": {
"$elemMatch": {
"$in": [
ObjectId("6081fed9ee5d133dbffb3cba")
]
}
}
}
}
}
}
}
])
I am trying to find the record such that the result looks like the following.
[{
"_id": {
"$oid": "6081fedbee5d133dbffb42eb"
},
"lessons": [
{
"_id": "460c42e1-b0b7-437e-ab63-c59cce8ced0d",
"name": "zzzz",
"subLesson": [
{
"$oid": "6081fed9ee5d133dbffb3cba"
},
{
"$oid": "6081fed9ee5d133dbffb3cc0"
}
]
}
]
},
}]
Can anyone please help out to understand how can I make this work
thanks
You can use $in directly
db.collection.aggregate([
{
$project: {
lessons: {
$filter: {
input: "$lessons",
cond: {
$in: [ ObjectId("6081fed9ee5d133dbffb3cba"), "$$this.subLesson" ]
}
}
}
}
}
])
Working Mongo playground
Update 1
db.collection.aggregate([
{ "$unwind": "$lessons" },
{
"$match": {
"lessons.subLesson": {
$in: [ ObjectId("6081fed9ee5d133dbffb3cba"), ObjectId("6081fed9ee5d133dbffb3cbb") ]
}
}
},
{
$group: {
_id: "$_id",
name: { $first: "$name" },
lessons: { $push: "$lessons" }
}
}
])
Mongo Playground

Incorrect count from aggregation query

In the following document collection, I am trying to find the total words of unique sentences. The total words must come out as 5 (hello\nworld, how are you?) + 5 (hello world, I am fine) + 3(Is it raining?) + 5(Look at the beautiful tiger!) = 18
[
{
"sourceList": [
{
"source": "hello\nworld, how are you?",
"_id": ObjectId("5f0eb9946db57c0007841153")
},
{
"source": "hello world, I am fine",
"_id": ObjectId("5f0eb9946db57c0007841153")
},
{
"source": "Is it raining?",
"_id": ObjectId("5f0eb9946db57c0007841153")
}
]
},
{
"sourceList": [
{
"source": "Look at the beautiful tiger!",
"_id": ObjectId("5f0eb9946db57c0007841153")
},
{
"source": "Is it raining?",
"_id": ObjectId("5f0eb9946db57c0007841153")
}
]
}
]
But with the below query
db.collection.aggregate([
{
"$unwind": "$sourceList"
},
{
$project: {
"sp": {
$split: [
"$sourceList.source",
"\n"
],
$split: [
"$sourceList.source",
" "
]
}
}
},
{
"$group": {
"_id": null,
"elements": {
$addToSet: "$sp"
}
}
},
{
"$unwind": "$elements"
},
{
"$project": {
"sizes": {
"$size": "$elements"
}
}
},
{
"$group": {
"_id": null,
"count": {
"$sum": "$sizes"
}
}
}
])
it gives as 17. What could be the reason for this? I am first trying to split by \n and then by space
EDIT
I am trying to find word count for unique sentences and total unique sentences.
The problem is that here:
"sp": {
$split: [
"$sourceList.source",
"\n"
],
$split: [
"$sourceList.source",
" "
]
}
only the second $split gets executed by MongoDB and it returns hello\nworld as one string. There's no such "cascade" syntax, since it's simply the same JSON key $split so last wins.
In order to fix that you can use $reduce to apply $split by whitespace on an array of split by \n values:
{
$project: {
"sp": {
$reduce: {
input: { $split: [ "$sourceList.source", "\n" ] },
initialValue: [],
in: { $concatArrays: [ "$$value", { $split: [ "$$this", " " ] } ] }
}
}
}
}
Mongo Playground
As per the comments and addition to #micki's answer and my previous answer,
play
db.collection.aggregate([
{
"$unwind": "$sourceList"
},
{
$project: {
"sp": {
$reduce: {
input: {
$split: [
"$sourceList.source",
"\n"
]
},
initialValue: [],
in: {
$concatArrays: [
"$$value",
{
$split: [
"$$this",
" "
]
}
]
}
}
}
}
},
{
"$group": {
"_id": null,
"elements": {
$addToSet: "$sp"
}
}
},
{
"$project": {
"unique_sen": {
"$size": "$elements"
},
"elements": 1
}
},
{
"$unwind": "$elements"
},
{
"$project": {
"sizes": {
"$size": "$elements"
},
"unique_sen": 1
}
},
{
"$group": {
"_id": null,
"unique_count": {
"$sum": "$sizes"
},
"data": {
$push: "$$ROOT"
}
}
},
{
"$project": {
"unique_count": 1,
"unique_sen": {
$first: "$data.unique_sen"
}
}
}
])
Update:
You don't need to escape in the query.
play
db.collection.aggregate([
{
"$match": {
"url": "https://www.rootsresource.in"
}
},
{
"$unwind": "$translations"
},
{
$project: {
"sp": {
$reduce: {
input: {
$split: [
"$translations.source",
"\n"
]
},
initialValue: [],
in: {
$concatArrays: [
"$$value",
{
$split: [
"$$this",
" "
]
}
]
}
}
}
}
},
{
"$group": {
"_id": null,
"elements": {
$addToSet: "$sp"
}
}
},
{
"$project": {
"unique_sen": {
"$size": "$elements"
},
"elements": 1
}
},
{
"$unwind": "$elements"
},
{
"$project": {
"sizes": {
"$size": "$elements"
},
"unique_sen": 1
}
},
{
"$group": {
"_id": null,
"unique_count": {
"$sum": "$sizes"
},
"data": {
$push: "$$ROOT"
}
}
},
{
"$project": {
"unique_count": 1,
"unique_sen": {
$first: "$data.unique_sen"
}
}
}
])
UPDATE:
Above query works from mongo 4.4 - $first is available in project from 4.4
For older versions.
db.test.aggregate([
{
"$match": {
url: "https://www.rootsresource.in"
}
},
{
"$unwind": "$translations"
},
{
$project: {
"sp": {
$reduce: {
input: {
$split: [
"$translations.source",
"\n"
]
},
initialValue: [],
in: {
$concatArrays: [
"$$value",
{
$split: [
"$$this",
" "
]
}
]
}
}
}
}
},
{
"$group": {
"_id": null,
"elements": {
$addToSet: "$sp"
}
}
},
{
"$project": {
"unique_sen": {
"$size": "$elements"
},
"elements": 1
}
},
{
"$unwind": "$elements"
},
{
"$project": {
"sizes": {
"$size": "$elements"
},
"unique_sen": 1
}
},
{
"$group": {
"_id": null,
"unique_count": {
"$sum": "$sizes"
},
"data": {
$push: "$$ROOT"
}
}
},
{
"$project": {
"unique_count": 1,
unique_sen: { $arrayElemAt: [ "$data.unique_sen", 0 ] }
}
}
])

Resources