MongoDB: Add date comparison to arrayFilters - mongodb

Objects of my collection have a field, that is an array of objects with one of the field being a string date
{
citizens: [{
name: 'John'
birthday: '1993/07/13'
},
{
name: 'Sarah'
birthday: '1996/07/13'
},
{
name: 'Natalia',
birthday: '2015/07/13'
}]
}
{
citizens: [{
name: 'Leo'
birthday: '1994/02/08'
},
{
name: 'Paul'
birthday: '1934/09/13'
},
{
name: 'Rego',
birthday: '2019/01/29'
}]
}
I want to set to all the users older than 18 status 'adult'
Here is what I try to do:
users.updateMany({}, {
$set: { 'citizens.$[elem].status': 'adult' },
},
{
arrayFilters: [
{ 'elem.status': { $exists: false } },
{ $lt: [{ $toDate: 'elem.$birthday' }, 18yearsaAgoDate] }, <-- 18years don't mean much here, I actually use $$NOW
],
multi: true,
});
But I get 'unknown top level operator: $lt' error when run this. How do I supposed to use $lt in arrayFilter?
Thanks in advance!

Here's how you could do it in a simple update using the aggregation pipelined updates:
db.collection.updateMany({},
[
{
$set: {
citizens: {
$map: {
input: "$citizens",
in: {
$mergeObjects: [
{
status: {
$cond: [
{
$gt: [
{
$dateDiff: {
startDate: {
$toDate: "$$this.birthday"
},
endDate: "$$NOW",
unit: "year"
}
},
18
]
},
"adult",
"$$REMOVE"
]
}
},
"$$this"
]
}
}
}
}
}
])
Mongo Playground
I've used some version 5+ operators like $dateDiff as it makes the code cleaner, but you could still achieve the same results without them using $subtract and a constant for 18 years, like so:
{
$lt: [
{
$toDate: "$$this.birthday"
},
{
$subtract: [
"$$NOW",
567648000000// 18 years in miliseconds
]
}
]
}
Mongo Playground

This is an update using the arrayFilters syntax.
db.collection.updateMany(
{ },
{
$set: { "citizens.$[elem].status": "adult" }
},
{
arrayFilters: [ { "elem.status": { $exists: false } , "elem.birthday": { $lt: "2004/07/27" } } ]
}
)
Note the date value "2004/07/27" is the day 18 years ago (very close approximate value). And using string values in date comparison requires that the value is formatted in "YYYY/mm/dd".

It would have worked like this if your date was already in the right format. Since you need to format it, I think you should use an aggregation pipeline with a $merge stage:
db.collection.aggregate([
{$set: {
citizens: {
$map: {
input: "$citizens",
in: {$mergeObjects: [
{status: {
$cond: [
{$lt: [{$toDate: "$$this.birthday"}, 18yearsaAgoDate]},
"adult",
"$$REMOVE"
]
}
},
"$$this"
]
}
}
}
}
},
{ $merge : { into : "collection" } }
])
See how it works on the playground example

Related

How to find prev/next document after sort in MongoDB

I want to find prev/next blog documents whose publish date is closest to the input document.
Below is the document structure.
Collection Examples (blog)
{
blogCode: "B0001",
publishDate: "2020-09-21"
},
{
blogCode: "B0002",
publishDate: "2020-09-22"
},
{
blogCode: "B0003",
publishDate: "2020-09-13"
},
{
blogCode: "B0004",
publishDate: "2020-09-24"
},
{
blogCode: "B0005",
publishDate: "2020-09-05"
}
If the input is blogCode = B0003
Expected output
{
blogCode: "B0005",
publishDate: "2020-09-05"
},
{
blogCode: "B0001",
publishDate: "2020-09-21"
}
How could I get the output result? In sql, it seems using ROW_NUMBER can solve my problem, however I can't find a solution to achieve the feature in MongoDB. The alternate solution may be reference to this answer (But, it seems inefficient). Maybe using mapReduce is another better solutions? I'm confused at the moment, please give me some help.
You can go like following.
We need to compare existing date with given date. So I used $facet to categorize both dates
The original data should be one Eg : B0003. So that I just get the first element of the origin[] array to compare with rest[] array
used $unwind to flat the rest[]
Substract to get the different between both dates
Again used $facet to find previous and next dates.
Then combined both to get your expected result
NOTE : The final array may have 0<elements<=2. The expected result given by you will not find out whether its a prev or next date if there is a one element. So my suggestion is add another field to say which date it is as the mongo playground shows
[{
$facet: {
origin: [{
$match: { blogCode: 'B0001' }
}],
rest: [{
$match: {
$expr: {
$ne: ['$blogCode','B0001']
}
}
}]
}
}, {
$project: {
origin: {
$arrayElemAt: ['$origin',0]
},
rest: 1
}
}, {
$unwind: {path: '$rest'}
}, {
$project: {
diff: {
$subtract: [{ $toDate: '$rest.publishDate' },{ $toDate: '$origin.publishDate'}]
},
rest: 1,
origin: 1
}
}, {
$facet: {
prev: [{
$sort: {diff: -1}
},
{
$match: {
diff: {$lt: 0 }
}
},
{
$limit: 1
},
{
$addFields:{"rest.type":"PREV"}
}
],
next: [{
$sort: { diff: 1 }
},
{
$match: {
diff: { $gt: 0 }
}
},
{
$limit: 1
},
{
$addFields:{"rest.type":"NEXT"}
}
]
}
}, {
$project: {
combined: {
$concatArrays: ["$prev", "$next"]
}
}
}, {
$unwind: {
path: "$combined"
}
}, {
$replaceRoot: {
newRoot: "$combined.rest"
}
}]
Working Mongo playground
Inspire for the solution of varman proposed. I also find another way to solve my problem by using includeArrayIndex.
[
{
$sort: {
"publishDate": 1
},
},
{
$group: {
_id: 1,
root: {
$push: "$$ROOT"
}
},
},
{
$unwind: {
path: "$root",
includeArrayIndex: "rownum"
}
},
{
$replaceRoot: {
newRoot: {
$mergeObjects: [
"$root",
{
rownum: "$rownum"
}
]
}
}
},
{
$facet: {
currRow: [
{
$match: {
blogCode: "B0004"
},
},
{
$project: {
rownum: 1
}
}
],
root: [
{
$match: {
blogCode: {
$exists: true
}
}
},
]
}
},
{
$project: {
currRow: {
$arrayElemAt: [
"$currRow",
0
]
},
root: 1
}
},
{
$project: {
rownum: {
prev: {
$add: [
"$currRow.rownum",
-1
]
},
next: {
$add: [
"$currRow.rownum",
1
]
}
},
root: 1
}
},
{
$unwind: "$root"
},
{
$facet: {
prev: [
{
$match: {
$expr: {
$eq: [
"$root.rownum",
"$rownum.prev"
]
}
}
},
{
$replaceRoot: {
newRoot: "$root"
}
}
],
next: [
{
$match: {
$expr: {
$eq: [
"$root.rownum",
"$rownum.next"
]
}
}
},
{
$replaceRoot: {
newRoot: "$root"
}
}
],
}
},
{
$project: {
prev: {
$arrayElemAt: [
"$prev",
0
]
},
next: {
$arrayElemAt: [
"$next",
0
]
},
}
},
]
Working Mongo playground

get a datetime from mongo and push it to the array

I have documents with the following props:
{
'published_date': '2020/03/10 07:20:09',
'relationships': [
{'rel_name': 'HAS_REL'},
{'rel_name': 'HAS_NO_REL'},
]
}
I want to add in each field of relationships that has as rel_name the value of HAS_REL the value of the published date as the property dict. The document will become as follows:
{
'published_date': '2020/03/10 07:20:09',
'relationships': [
{ 'rel_name': 'HAS_REL'
'date': 2020,03,10,07,20,09
},
{'rel_name': 'HAS_NO_REL'},
]
}
So far my query looks something like this:
TEST_COLLECTION.update_one(
{'_id': ObjectId(document_id)},
{'$set': {'relationships.$[elem].date': {'$dateFromString': '$published_date'}}},
False,
False,
None,
[{'elem.rel_name': 'HAS_RELATIONSHIP'}],
)
But I'm getting the error:
WriteError: The dollar ($) prefixed field '$dateFromString' in 'parsed.relationships.1.date.$dateFromString' is not valid for storage.
any ideas?
UPDATE
With the initial answer, I've updated the query such as follows:
TEST_COLLECTION.update_one(
{'_id': ObjectId(document_id)},
[
{'$set': {
'relationships': {
'$let': {
'vars': {
'date': { '$dateFromString': { 'dateString': '$published_date', format: "%Y/%m/%d %H:%M:%S" } }
},
'in': {
'$map': {
'input': "$relationships",
'in': {
'$cond': {
'if': { '$eq': ["$$this.rel_name", "HAS_REL"] },
'then': { '$mergeObjects': ["$$this", { 'date': "$$date" }] },
'else': "$$this"
}
}
}
}
}
}
}
}
]
)
However, it seems I'me not getting a correct document:
InvalidDocument: documents must have only string keys, key was
built-in function format
Have a look at $dateFromString
You have to specify format field, unless you use default format "%Y-%m-%dT%H:%M:%S.%LZ". Storing date/time values as string is usually a design flaw.
So, it must be
{'$dateFromString': { dateString: '$published_date', format: "%Y/%m/%d %H:%M:%S" } }
Note, the time is considered at UTC time. Set field timezone to specify the time zone if required.
Be aware, $dateFromString() is an aggregation function, so you must use
TEST_COLLECTION.update_one(
{ '_id': ObjectId(document_id) },
[
{
$set: {
relationships: {
$map: {
input: "$relationships",
in: {
$cond: {
if: { $eq: ["$$this.rel_name", "HAS_REL"] },
then: {
$mergeObjects: [
"$$this",
{
date: {
$dateFromString: {
dateString: '$published_date',
format: "%Y/%m/%d %H:%M:%S"
}
}
}
]
},
else: "$$this"
}
}
}
}
}
}
]
)
or another style:
TEST_COLLECTION.update_one(
{ '_id': ObjectId(document_id) },
[
{
$set: {
relationships: {
$let: {
vars: {
date: { $dateFromString: { dateString: '$published_date', format: "%Y/%m/%d %H:%M:%S" } }
},
in: {
$map: {
input: "$relationships",
in: {
$cond: {
if: { $eq: ["$$this.rel_name", "HAS_REL"] },
then: { $mergeObjects: ["$$this", { date: "$$date" }] },
else: "$$this"
}
}
}
}
}
}
}
}
]
)

Modify a field of all documents by appending time in the 'hh: mm A' format

These are the documents I have inside a collection:
[
{
"unix_date": 1582133934,
"text": "mongo"
},
{
"unix_date": 1580068560,
"text": "some"
},
]
I want to change the text field of all documents so that they look this way:
[
{
"unix_date": 1582133934,
"text": "mongo 12:00 PM"
},
{
"unix_date": 1580068560,
"text": "some 3:00 PM"
},
]
Note that I used random times.
This is what I tried:
db.collection.update({}, [{
$set: {
text: {
$concat: ["$text", new Date("$unix_date" * 1000).toString()]
}
}
}], {
multi: true
})
this is appending invalid date to the text field and even if it does append the correct string how can I format it to hh: mm AM/PM. Is this possible without using any external libraries? I want to do this directly inside the shell.
The reason it's failing is cause you can't execute .Js logic in mongo query like that, try as below :
db.collection.update(
{},
[
{
$set: {
text: {
$concat: ["$text", " ", {
$let: {
vars: {
hourMins: { $dateToString: { format: "%H:%M",date: { $toDate: { $multiply: ["$unix_date",1000]}},timezone: "America/Chicago"}},
hour: { $hour: { date: { $toDate: { $multiply: [ "$unix_date", 1000 ] } }, timezone: "America/Chicago" } } },
in: { $concat: [ "$$hourMins", " ", { $cond: [ { $lte: [ "$$hour", 12 ]}, "AM", "PM" ] } ] } }
}]
}
}
}
],
{
multi: true,
}
);
Ref : aggregation-pipeline
Test : mongoplayground

how to check a date is less than today date in mongoose?

my collection in db is
plannedEndDate:{"2020-03-10T11:22:33.677+00:00"}
in controller var tdate= new Date(); [tdate is in the format 2020-03-10T14:28:22.687Z].
now,I need to check plannedEndDate is less than tdate in mongoose.
i tried,
plannedEndCmp: {
$cond: [{ $lt: ["$plannedEndDate", tdate] }, 1, 0]
}
but it is not returning true.
so should i need to trim the timestamp to compare dates in mangodb?or should i need to convert date into common format?
Not clear what you actually like to do but below command find the document or show indication:
db.collection.find(
{ plannedEndDate: { $lt: tdate } }
)
db.collection.aggregate([
{ $set: { plannedEndCmp: { $cond: [{ $lt: ["$plannedEndDate", tdate] }, 1, 0] } } },
{ $set: { plannedEndCmp_bool: { $lt: ["$plannedEndDate", tdate] } } }
])
In Aggregate group:
`
factualEndDate: {
"$dateToString": { "format": "%Y-%m-%d", "date": "$factualEndDate" }
},
In Aggregate project:
DelayedComplete: { $cond: [
{
$and: [
{ $cond: [{ $lte: [{ $max: "$data.factualEndDate" }, today] }, 1, 0] },
]
},
1,
0
]
},
`

Returning a document with two fields from the same array in MongoDB

Given documents such as
{
_id: 'abcd',
userId: '12345',
activities: [
{ status: 'login', timestamp: '10000001' },
{ status: 'logout', timestamp: '10000002' },
{ status: 'login', timestamp: '10000003' },
{ status: 'logout', timestamp: '10000004' },
]
}
I am trying to create a pipeline such as all users that have their latest login/logout activities recorded between two timestamps will be returned. For example, if the two timestamp values are between 10000002 and 10000003, the expected document should be
{
_id: 'abcd',
userId: '12345',
login: '10000003',
logout: '10000002'
}
Of if the two timestamp values are between -1 and 10000001, the expected document should be :
{
_id: 'abcd',
userId: '12345',
login: '10000001',
logout: null
}
Etc.
I know it has to do with aggregations, and I need to $unwind, etc., but I'm not sure about the rest, namely evaluating two fields from the same document array
You can try below aggregation:
db.col.aggregate([
{
$unwind: "$activities"
},
{
$match: {
$and: [
{ "activities.timestamp": { $gte: "10000001" } },
{ "activities.timestamp": { $lte: "10000002" } }
]
}
},
{
$sort: {
"activities.timestamp": -1
}
},
{
$group: {
_id: "$_id",
userId: { $first: "$userId" },
activities: { $push: "$activities" }
}
},
{
$addFields: {
login: { $arrayElemAt: [ { $filter: { input: "$activities", as: "a", cond: { $eq: [ "$$a.status", "login" ] } } } , 0 ] },
logout: { $arrayElemAt: [ { $filter: { input: "$activities", as: "a", cond: { $eq: [ "$$a.status", "logout" ] } } } , 0 ] }
}
},
{
$project: {
_id: 1,
userId: 1,
login: { $ifNull: [ "$login.timestamp", null ] },
logout: { $ifNull: [ "$logout.timestamp", null ] }
}
}
])
We need to use $unwind + $sort + $group to make sure that our activities will be sorted by timestamp. After $unwind you can use $match to apply filtering condition. Then you can use $filter with $arrayElemAt to get first (latest) value of filtered array. In the last $project you can explicitly use $ifNull (otherwise JSON key will be skipped if there's no value)
You can use below aggregation
Instead of $unwind use $lte and $gte with the $fitler aggregation.
db.collection.aggregate([
{ "$project": {
"userId": 1,
"login": {
"$max": {
"$filter": {
"input": "$activities",
"cond": {
"$and": [
{ "$gte": ["$$this.timestamp", "10000001"] },
{ "$lte": ["$$this.timestamp", "10000004"] },
{ "$lte": ["$$this.status", "login"] }
]
}
}
}
},
"logout": {
"$max": {
"$filter": {
"input": "$activities",
"cond": {
"$and": [
{ "$gte": ["$$this.timestamp", "10000001"] },
{ "$lte": ["$$this.timestamp", "10000004"] },
{ "$lte": ["$$this.status", "logout"] }
]
}
}
}
}
}}
])