I am using https://www.npmjs.com/package/mongodb
mongodb has turned my timestamp into BSON (I know this is normal)
my timestamp is this 641912491 where as in the database it looks like this 1457904327000.000000.
I have a document with a key named date_in_the_past which holds my timestamp in it's BSON form.
Note: I am fully aware that the mongodb _id key holds a date but it is of no use to me in this scenario as, that would be the date when the document was written to the db (not the date in the past that I am looking for)
How can I convert my normal timestamp into a BSON timestamp?
I have tried to understand mongodb/js-bson and mongodb/js-bson/blob/master/lib/bson/timestamp.js but I can't see how to do it.
example of what I am looking to do:
var past=timestampToBSON(641912491);
db.collection('docs').find({date_in_the_past:past}).limit(1).toArray(function(e,r){});
If it's stored as a bson timestamp object, you could do something like this
var Timestamp = require('mongodb').Timestamp;
var past = Timestamp(641912491,1);
db.collection('docs').find({date_in_the_past:past})
or
db.collection('docs').find({date_in_the_past: Timestamp(641912491,1)})
it has some more info and examples about it here
https://docs.mongodb.org/manual/reference/bson-types/#timestamps
Related
I logged out the value stored in one of the documents(or notes in my case) in the date key, like this
console.log(notes[0].date)
The output that I got was
and when methods like getDate() or toDateString() didn't work, what should i be doing?
You will need to convert it into a javascript date object using new Date("your mongodb date string");
See: https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Date for more infomation
If your dates are stored using the date data type you should manipulate them as data object in your code.
If your dates are not stored using the date data type you should migrate your data to store the date as date data type rather than string.
You then format the date, using the user’s LOCALE, only when you present it to the user.
I'm struggling to understand the logic behind MongoDB date formatting, if I save a field like so:
date: Date.now()
in the database looks like:
date: 1633186879027
I have a date like 2021-08-27T19:00:38.000+00:00 , and I save it like so:
date: "2021-08-27T19:00:38.000+00:00"
in the database looks like:
date: "2021-08-27T19:00:38.000+00:00"
and it's not what I want cause it's a string and I can't sort stuff by date then.
so i tried to save it like so:
date: new Date("2021-08-27T19:00:38.000+00:00")
and in the database looks like:
date: 2021-08-27T19:00:38.000+00:00
without brakets, as Mongodb does for strings, so must not be a string either
how am I supposed to save it so that it looks like the first one (1633186879027)?
because I then need to sort stuff by date and I think that's the correct format to use?
MongoDB stores data using BSON. The definition is here
A datetime is stored as the number of milliseconds since 1970-01-01 using a 64-bit integer.
Date.now() returns an integer, so the sample data would be stored as BSON type \x12 with the value 1633186879027.
new Date("2021-08-27T19:00:38.000+00:00") would be stored as BSON type \x09 with the value 1630090838000.
The output date: 2021-08-27T19:00:38.000+00:00 was generated by the driver or application on the client side after it was retrieved.
MongoDB directly support sorting on dates.
Also note that if you have dates stored as strings like "2021-08-27T19:00:38.000+00:00", a lexicographical sort of those string would put them in chronological order.
I try to dynamically create a mongo query with dates.
This is a part of a JSON query I produced:
{"$or":[{"createdAt":{"$gte":"2017-08-31T22:00:00.000Z"}},{"modifiedAt":{"$gte":"2017-08-31T22:00:00.000Z"}}]}
but it does not work.
This is a part of code which stands behind it:
let result = {$or: [{createdAt: {$lte: new Date(date)}}, {modifiedAt: {$lte: new Date(date)}}]};
Spent 3 hours trying to find a solution. My understanding is that $lte here tries to compare mongo date object with a string describing date in ISO format. And it does not work well.
I do not know how to create a proper JSON object being a mongo query containing dates.
Please help! :-)
If you want to query Mongo with date range, you have to use a standard Date format (ISO-8601). To achieve this, try with moment:
let date ="2019-02-22"; //for example, in my apis, you can set multiple type of date (YYYY-MM-DD, YYYYMMDD, DD-MM-YYYY,..) but date format with / is forbidden.
let query = {$or:[{createdAt: {$lte: moment(date).format()}}, {modifiedAt:{$gte: moment(date).format()}}] //pass parameter to format, in this case it will use the default locale format
Like this you are setting a query. IMO querying on date range with "OR" condition is not useful: usually a date query in is "AND" condition (if you are querying on a single date, your query will be: greater than today or lower than today -> everything)
I try to store some Date on mongo Collections.
When I do let date = new Date(); MaCol.insert({'date': date});, I can see on my mongo collection that the date is stored with the correct timezone
(something like : 2017-05-22 12:42:28.441Z).
But if I use a value on constructor of Date like let date = new Date('03/03/2000) i lose the time zone : 2000-03-03 00:00:00.000Z
How to correct that ?
Thanks.
You should use moment.js to make this a breeze. They support a ton of options. In your case, using moment js, the below should work:
moment('03/03/2000').format();
>> "2000-03-03T00:00:00+05:30"
You can test other options by just visiting the momentjs page, opening up the dev console and directly typing in the above (or other format commands) to see if it gets you what you neeed.
In This Meteor server side code for the users collections, the property createdAt shows something like this ISODate("2017-02-09T01:22:30.894Z").
And in another collection myCol I have the createdAt property with the unix timestamp in milliseconds.
Moment.js is installed.
How can I check the following condition:
myCol.createdAt is after n months from the end of the month when the user was created. thx
Here is one approach that should work (there are of course a handful of other ways to do this...this was the first one that came to mind).
You can convert the Users createdAt property to a moment object like this (assuming your user doc is stored in a var called userOne).
var moment1 = moment(userOne.createdAt);
Then, you can convert the unix timestamp in the other collection like this (assuming the doc is stored in a var called doc).
var moment2 = moment(doc.createdAt);
Now find the end of the moment for moment1 and add in 'N' months.
moment1.endOf('month').add(N, 'months');
Finally, do your comparison.
moment2.isAfter(moment1);
I'm not sure how to get that string representation of the ISODate object, perhaps just .toString().
However, this is how you can get the time difference in months with the myCol.createdAt represented by the current date Date.now().
var isoString = "2017-02-09T01:22:30.894Z"
var today = Date.now()
console.log(moment(isoString).diff(today, 'months'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.0/moment.min.js"></script>