mongodb timestamp to date - mongodb

i am pretty new to mongodb. i have a collection or json file like below.
{
"id": {
"timestamp": 1592538583,
"machineIdentifier": 1772242,
"processIdentifier": -7129,
"counter": 2887223,
"timeSecond": 1592538583,
"time": 1592538583000,
"date": 1592538583000
},
"creationTimestamp": 1592538583524,
"lastUpdateTimestamp": 1592538642832,
"idAsString": "5eec35d71b0ad2e4272c0e37"
}
i need to extract records based on timestamp. when i give below format its working. But i need human readable format. like lastUpdateTimestamp greater than "2020-06-30T00:00:00Z". i tried many waysbut getting bson format errors. any suggestions?
{ "lastUpdateTimestamp": { $gt : new Date(1592282308044) }}

Try to use it as a number.
{ "lastUpdateTimestamp": { $gt : 1592282308044 }}

Related

Insert document with datetime field using the JSON View

Context
On MongoDB Atlas, using the JSON view:
Question
I want to insert a document which has a datetime field. I think I need to use the ISO 8601 format. But I've no idea how it should be formated. The MongoDB documentation doesn't tell a lot about it. I want to do it like this because I'm using the output of a python script but is it even possible?
Following is working:
db.test.insert({ _id:ObjectId("6027dd515ce9cb94d55744b0") , created_at:ISODate("2021-02-13T15:02:21.565762") })
WriteResult({ "nInserted" : 1 })
db.test.find()
{ "_id" : ObjectId("6027dd515ce9cb94d55744b0"), "created_at" : ISODate("2021-02-13T15:02:21.566Z") }
It can be done like this:
{
"_id": {
"$oid": "60280eb44567b5fc13d18d1e"
},
"created_at": {
"$date": {
"$numberLong": "1613224941565"
}
}
}

Mongodb Query to get only documents in specific days

In my mongodb table, I have 2 (relevant for this Q) columns: service, timestamp.
I want to query only rows with service=liveness and that those with timestamp of 12th Novermber 2020.
How can I do it, if timestamp field is of type Number (UNIX epoch number)..?
This is my query currently:
{ service: "liveness" }.
This is how the timestamp column looks like:
To query by two fields you only need this syntax:
db.collection.find({
"field1": yourField1Value,
"field2": yourField2Value
So, if your date is a Number instead of a Date you can try this query:
db.collection.find({
"service": "liveness",
"timestamp": 1600768437934
})
And should works. Example here.
Now, if the problem is parse 12th November 2020 to UNIX timestamp, then the easiest way is convert first the date in your app language.
Edit:
Also, I don't know if I've missunderstood your question but, here is another query.
db.collection.aggregate([
{
"$match": {
"service": "liveness",
}
},
{
"$project": {
"timestamp": {
"$toDate": "$timestamp"
}
}
},
{
"$match": {
"timestamp": {
"$gt": ISODate("1990-01-01"),
"$lt": ISODate("2060-01-01")
}
}
}
])
This query first match all documents with service as liveness, so the next stage is faster. Into $project the timestamp is parsed to Date so you can match again with your date.
Using $gt and $lt you can search by a whole day.
And also, if you can get the days into UNIX timestamp you can do this:
db.collection.find({
"service": "liveness",
"timestamp": {
"$gte": yourDay,
"$lt": nextrDay
}
})
Using $gte and $lt you ensure the query will find all values in the day.

MongoDB Project - return data only if $elemMatch Exist

Hello Good Developers,
I am facing a situation in MongoDB where I've JSON Data like this
[{
"id": "GLOBAL_EDUCATION",
"general_name": "GLOBAL_EDUCATION",
"display_name": "GLOBAL_EDUCATION",
"profile_section_id": 0,
"translated": [
{
"con_lang": "US-EN",
"country_code": "US",
"language_code": "EN",
"text": "What is the highest level of education you have completed?",
"hint": null
},
{
"con_lang": "US-ES",
"country_code": "US",
"language_code": "ES",
"text": "\u00bfCu\u00e1l es su nivel de educaci\u00f3n?",
"hint": null
}...
{
....
}
]
I am projecting result using the following query :
db.collection.find({
},
{
_id: 0,
id: 1,
general_name: 1,
translated: {
$elemMatch: {
con_lang: "US-EN"
}
}
})
here's a fiddle for the same: https://mongoplayground.net/p/I99ZXBfXIut
I want those records who don't match $elemMatch don't get returned at all.
In the fiddle output, you can see that the second item doesn't have translated attribute, In this case, I don't want the second Item at all to be returned.
I am using Laravel as Backend Tech, I can filter out those records using PHP, but there are lots of records returned, and I think filtering using PHP is not the best option.
You need to use $elemMatch in the first parameter
db.collection.find({
translated: {
$elemMatch: {
con_lang: "IT-EN"
}
}
})
MongoPlayground

MongoDB projecting an entire subdocument

I've got a bunch of documents like this in my collection.
{
"success": true,
"timestamp": 1519296206,
"base": "EUR",
"date": "2018-07-04",
"rates": {
"AUD": 1.566015,
"CAD": 1.560132,
"CHF": 1.154727,
"CNY": 7.827874,
"GBP": 0.882047,
"JPY": 132.360679,
"USD": 1.23396,
}
}
I would like to only get date and the entire rates subdocument like below. I know I could add rates.AUD, rates.CAD etc. to the projection but that would make the projection extremely big and just unbearable to read and hard to maintain as a new field (or currency in this case) might get added in the future.
{
"date": "2018-07-04",
"rates": {
"AUD": 1.566015,
"CAD": 1.560132,
"CHF": 1.154727,
"CNY": 7.827874,
"GBP": 0.882047,
"JPY": 132.360679,
"USD": 1.23396,
}
}
Is there any projection similar to {date: 1, "rates.*". 1} that works like described above?
Maybe this?
db.col.aggregate([ {
$project: {
date: 1,
rates: 1
}
}])

MongoDB - Query Available Date Range with a Date Range for Hotel

I have an array of objects containing dates of when a hotel is available to book within Mongo. It looks something like this, using ISO Date formats as said here.
Here's what document looks like, trying to keep it short for the example.
available: [
{
"start":"2014-04-07T00:00:00.000000",
"end":"2014-04-08T00:00:00.000000"
},
{
"start":"2014-04-12T00:00:00.000000",
"end":"2014-04-15T00:00:00.000000"
},
{
"start":"2014-04-17T00:00:00.000000",
"end":"2014-04-22T00:00:00.000000"
},
]
Now, I need query two dates, check in date and check out date. If the dates are available, Mongo should return the document, otherwise it won't. Here are a few test cases:
2014-04-06 TO 2014-04-08 should NOT return.
2014-04-13 TO 2014-04-16 should NOT return.
2014-04-17 TO 2014-04-21 should return.
How would I go about forming this in to a Mongo query? Using $elemMatch looked like it would be a good start, but I don't know where to take it after that so all three examples I posted above work with the same query. Any help is appreciated.
db.collection.find({
"available": {
"$elemMatch": {
"start": { "$lte": new Date("2014-04-17") },
"end": { "$gte": new Date("2014-04-21") }
}
}
})
How about this command?
Well I actually hope your documents have real ISODates rather than what appears to be strings. When they do then the following query form matches as expected:
db.collection.find({
"available": {
"$elemMatch": {
"start": { "$gte": new Date("2014-04-17") },
"end": { "$gte": new Date("2014-04-21") }
}
}
})