Querying embedded documents by matching dates in MongoDB - regex

I have a database containing emails and I want to search for a specific date using the regex operator and return all emails sent on that date. I have tried this so far but it doesn't return anything. I am new to regex and am not sure if I'm querying the date correctly.
db.messages.find({'headers.Date' : $regex : '2001-07-06'}})
This example below successfully returned all the emails send from the specified email address.
db.messages.find({'headers.From' : { $regex : 'reservations#merriotts.com' } });
The emails contain the following information:
headers { content transfer encoding, content type, date, from, message id, mime version, subject, to }

You need not make use of regex here, something simpler like this should work:
db.posts.find({"headers.Date": new Date(2001, 06, 06) })
This should work if the dates you saved in the DB are without time (just day, month, year)
Now if you have dates saved with new Date(), which includes the time components as well, then you need to create a date range which includes all the moments for that day :
db.posts.find( //query for all moments/time of a specific date
{"headers.Date": {"$gte": new Date(2001, 6, 6), "$lt": new Date(2001, 6, 7)}})
Note - The API for Date is Date(YYYY,MM,DD) and counting for 'month' starts from '0' and counting for 'date' starts from '1'.

Related

Dialogflow - Using RegEx Entity with other entitiy in Traning Phrase gives empty value for the parameter

I am using a Regular Expression for the date format (DD/MM/YYYY) validation.
If I have the training phrase as 22/05/2021 it works perfectly.
But I need to have the training phrase as 22/05/2021 sample#sample.com. When I use like that for the date it gives empty value as shown in the below image.
if I use #sys.date for the date field it not always gives the correct format I need.
As an example, if I need the value 3rd of June, 2021 and enter it as 03/06/2021, Dialogflow evaluates it as 2001-03-06 (6th of March, 2021). But I need it as 03/06/2021
The locale I have to use with my agent is English-en
Updated:
Not even for date validation, this is not working for any regular expression combined with email as in the below image.

Mongoose/MongoDB regex find query using variable

I'm trying to perform a find query in mongoDB (using the mongoose framework with nodejs and express) using a regex expression based on a variable. I'm partially able to query correctly using a static string hard-coded into the code but I need to perform the query using a variable who's value is constantly changing.
The query uses three fields (author, date and updated date) and should go through all documents in a scheme and find all documents where 'date' or 'updated date' is like the variable currentDate (always formatted as YYYY-mm-dd) and where author is authorname.
The main issue is that mongoDB stores the dates with ISO format. On execution the string variable (currentDate (YYYY-mm-dd)) is formatted to ISO which leads it to only "hit" documents with time 00:00.000Z as ISO formats 2015-09-17 to be 2015-09-17 00:00:00.000Z (not taking timezone into consideration).
The query:
var currentDate = yyyy-mm-dd;
Scheme.find({$and:[{'local.author': author},
{$or: [{'local.date': new RegExp(currentDate)},
{'local.updated_date':new RegExp(currentDate)}]}]}
I've also tried with the $gte and $lte variables (where $gte = date today and &lte = date tomorrow (00:00)) but I couldn't get it to work, I met the wall trying to perform the regex.
I hope there is a brilliant regex expert out there who can help, thank you! :)
I figured it out without all the complexity.
var tomorrow = new Date();
var today = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
tomorrow.setHours(0,0,0,0);
today.setHours(0,0,0,0);
Scheme.find({$and:[{'local.author': author},{$or: [{'local.date': { $gt: today, $lt: tomorrow } },{'local.updated_date':{ $gte: today, $lt: tomorrow }}]}]}

Regex QueryString Parsing for a specific in BigQuery

So last week I was able to begin to stream my Appengine logs into BigQuery and am now attempting to pull some data out of the log entries into a table.
The data in protoPayload.resource is the page requested with the querystring paramters included.
The contents of protoPayload.resource looks like the following examples:
/service.html?device_ID=123456
/service.html?v=2&device_ID=78ec9b4a56
I am getting close, but when there is another entry before device_ID, I am not getting it. As you can see I am not great with Regex, but it is the only way I think I can parse the data in the query. To get just the device ID from the first example, I was able to use the following example. Works great. My next challenge is to the data when the second parameter exists. The device IDs can vary in length from about 10 to 26 characters.
SELECT
RIGHT(Regexp_extract(protoPayload.resource,r'[\?&]([^&]+)'),
length(Regexp_extract(protoPayload.resource,r'[\?&]([^&]+)'))-10) as Device_ID
FROM logs
What I would like is just the values from the querystring device_ID such as:
123456
78ec9b4a56
Assuming you have just 1 query string per record then you can do this:
SELECT REGEXP_EXTRACT(protoPayload.resource, r'device_ID=(.*)$') as device_id FROM mytable
The part within the parentheses will be captured and returned in the result.
If device_ID isn't guaranteed to be the last parameter in the string, then use something like this:
SELECT REGEXP_EXTRACT(protoPayload.resource, r'device_ID=([^\&]*)') as device_id FROM mytable
One approach is to split protoPayload.resource into multiple service entries, and then apply regexp - this way it will support arbitrary number of device_id, i.e.
select regexp_extract(service_entry, r'device_ID=(.*$)') from
(select split(protoPayload.resource, ' ') service_entry from
(select
'/service.html?device_ID=123456 /service.html?v=2&device_ID=78ec9b4a56'
as protoPayload.resource))

Dynamics Nav (Navision) webservice ReadMultiple date filter

Using the Navision webservices, how can you filter by a date.
i.e. Within a SalesHeader table there is an "ExportedDate". I would like to find all SalesHeaders where the ExportedDate has not been set or were exported on a particular date.
It seems that whenever we set a filter on a date field, then the webservice will either return all rows or no rows.
This can be done. You have to use the same filter expression as you would use in the Nav Client:
01012011.. would be all dates from 01.01.2011
..01012011 would be all dates to 01.01.2011
01012011..03012011 gets all dates between 01. and 03.
After publishing page 42 (Sales Order) as a web service in NAV, I added a web reference to the newly created web service in my Visual Studio project. In the C# code, I create a new instance of the service, and tell it to use the default credentials:
SalesOrders_Service salesOrdersService = new SalesOrders_Service();
salesOrdersService.UseDefaultCredentials = true;
Then I instantiate a filter, and set the field and criteria:
SalesOrders_Filter filter = new SalesOrders_Filter();
filter.Field = SalesOrders_Fields.Document_Date;
filter.Criteria = "01-31-14|''"; // specific date (MM-dd-yy) or empty
The filter instance is then added to a new array of SalesOrders_Filters before passing the latter to ReadMultiple:
SalesOrders[] salesOrders = salesOrdersService.ReadMultiple(new SalesOrders_Filter[] { filter }, null, 0);
On my machine, this returns two orders whose Document Date is 31 January 2014, and one order with a blank Document Date.

Rails 4 + MongoDB + Search query LIKE does not give correct output

In Rails, I am trying to fetch data from mongodb using LIKE query by providing regular expression but even though not getting the correct output.
Model : User
_id, name, display_name, age, address, nick_name
a1, Johny, Johny K, 12, New York, John
b1, James, James Waltor, 15, New York, James
c1, Joshua, Joshua T, 13, California, Josh
Now I have 3 set of records.
Query 1 : Search User having 'Jo' as keyword in initial name
User.where(name: /^jo/i)
Output - Only One record - instead of two.
Query 2 :- Match the text with all column values
User.where($where: /^jo/i)
Not getting the proper output.
Ok on the Query 1, can you output the documents. I believe one of your records in 'name' has a character in front of it such as white space. I just run the same query locally and it pulled multiple records back.
Try this:
User.where(name/(.*)jo(.*)/i).count and see what that returns. It should match 2. If that works, then you'll need to look at what is incorrect with the store value.
On Query 2, where have you seen this syntax. The $where is expecting a string of a js function to execute to match records. In your case to match any field within the document with an expression you would need to do a recursive function across each field in each document.
For Query 2 to match against all fields
One solution, although inefficient, is to do it within the Rails app instead of Mongodb query.
e.g.
User.all.select do | user | user.attributes.values.grep(/^jo/i).any? end