Parse Server aggregate using match on date column - mongodb

I'm using parse-server version 2.7.4 with parse (the JavaScript interface) version 1.11.1. I'm trying to execute an aggregate query using match on a date column. Here's what my pipeline looks like:
[
{
match: {
'_created_at': new Date('2018-04-01T00:00:00.000Z')
}
}
]
I most definitely have records in the database greater than that date, yet this query returns me zero records in JavaScript. However, if I run it in the Mongo shell, I get documents returned. Here is what I execute in the Mongo shell:
[
{
$match: {
'_created_at': ISODate('2018-04-01T00:00:00.000Z')
}
}
]
Am I doing something wrong with the parse JavaScript API or does it not support match aggregation on date columns? Thanks for any help.

Related

$match comparing one date field to another in mongoDB aggregate query

I have a field called file_date, and in the start of the query I've set a new field called start_date which is 10 days back using:
{ $set: { start_date:{ $subtract: [ "$$NOW", 10*24*60*60*1000 ] } } }.
I want to use $match to find all the results where file_date is bigger then start_date.
I've tried using $gte but I can't seem to get the right syntax.
I'm using mongo 4.2 so I cant use SubstructDate
Thanks for any help
Changing the dates to strings using $toString.
using $gte

Combination of and or in mongo db query

I am trying to debug and issue with my mongo db queries, the one that I m trying to use now looks like this:
mongo query {
'$and': {
'$or': { name: /aaaq/i, 'metadata.description': /aaaq/i },
'$and': {
'admin.parentPath': '3e98c9f0-cbe9-4c27-b1af-ba43069907cc',
'admin.sharedWith': '78b43de0-47c2-495b-a3ad-afc9fb6c9815'
}
}
}
The ide is that I want to check if the name or metadata respect the same regex expression and if the admin.parentPath and admin.sharedWith have the correct value, is this query written correctly?
The initial query looked like this:
mongo query {
name: /aaaq/i,
'metadata.description': /aaaq/i,
'admin.parentPath': '3e98c9f0-cbe9-4c27-b1af-ba43069907cc',
'admin.sharedWith': '78b43de0-47c2-495b-a3ad-afc9fb6c9815'
}
But I think that this one put AND between all conditions

How can I make a time-relative mongodb view pipeline?

I want to make a mongodb view called "orders4H" that always returns documents from our "orders" collection that have a createdAt field value that is <= NOW-4hours. Then, any tool that is using the "orders4H" view as a collection source will always get the most current order data for the last 4 hours. The view creation requires an aggregation pipeline. However, I cannot figure out how to make a pipeline that will $match on a relative date.
In mongodb 4.2 they added an aggregation system variable called NOW which seems like it would be the thing to use, if only I could get it to work in a query document. I can use it to make new fields, for example, if I put timeNow: "$$NOW" in a $addFields stage, I get a new field with the current datetime. Now I want to use this "$$NOW" in a query evaluation.
Short version of question - how can I write a $match pipeline stage that will perform a server-time-relative comparison with a datetime field from the document?
MongoDB tickets imply that this support has been added ... but I cannot figure out how to use the $$NOW in a query. Anyone out there know how to do this?
Here are the relevant tickets that I found:
https://jira.mongodb.org/browse/SERVER-37713
https://jira.mongodb.org/browse/SERVER-23656
With help from mongodb tech support, I was able to get a $match stage that works with mongodb 4.2 to make a time-relative query:
[
{ $match:
{ $expr:
{ $gt: [
"$_created_at",
{ $subtract: [ "$$NOW", 4 * 60 * 60 * 1000] } ]
}
}
}
]

How to extract the creation date of _id and add it as a new field using the aggregation framework?

I have been trying for a while to extract the insertion date of a mongodb document and add it as a new field to the same document.
I'm trying to do it using the mongo and mongo shell aggregation framework without getting good results.
Here is my query
db.getCollection('my_collection').aggregate(
[
{
$match: {
MY QUERY CRITERIA
}
},
{
$addFields: { "insertTime": "$_id.getTimestamp()" }
}
]
)
I am trying to extracr insertion time from _id using the function getTimestamp() but for sure there is somtehing about aggregation framework syntax that I am missing because I can not do what I am trying to do in my query.
This works perfect:
ObjectId("5c34f746ccb26800019edd53").getTimestamp()
ISODate("2019-01-08T19:17:26Z")
But this does not work at all:
"$_id.getTimestamp()"
What I am missing?
Thanks in advance

Improper date format in MongoDB Spring Data query criteria

I have an issue with a MongoDB query that I am having challenges with. In my class, I have a structure of [instance].events.destination.estimatedDateTime, and the value of estimatedDateTime is an ISO Date (eg. 2014-09-23 21:48:00.000Z). We are using org.springframework.data.mongodb.core.query.Criteria to append criteria to our query like this:
criteria = criteria.and("events.destination.estimatedDateTime").gte(today)
When this is generated, it looks like this when evaluated at runtime:
"events.destination.estimatedDateTime" : { "$gte" : { "$date" : "2014-10-28T05:00:00.000Z"} }
Unfortunately that is returning no records. I have taken that same condition and attempted to pull records in RoboMongo, but again there are no results. However, I have found that when I do the following in RoboMongo, I do indeed get results
"events.destination.estimatedDateTime" : { $gte : ISODate("2014-10-28T05:00:00.000Z") }
Is there a way using the Criteria class to generate ISODate(date) rather than "$date":date?