louischatriot / nedb

The JavaScript Database, for Node.js, nw.js, electron and the browser
MIT License
13.51k stars 1.03k forks source link

Query document by date #383

Open alanpachuau opened 8 years ago

alanpachuau commented 8 years ago

I have my persons document stored something like this

{"name":"Alan Pachuau","birthday":{"$$date":823631400000},"_id":"NmkakE7aHmJ19Xv5"} {"name":"John Pachuau","birthday":{"$$date":623631400000},"_id":"NmkakE7aHmJ19Xv5"}

I want to query the documents using the birthday field. I am trying to find person having birthday today. I couldn't find how to do this in the documentation. Any help would be appreciated.

Thank you

frenchbread commented 8 years ago

Try to store birthday's timestamp as a number. E.g.:

{ "name" : "Alan Pachuau", "birthday" : 823631400000 }, "_id" : "NmkakE7aHmJ19Xv5"}

This way, you can also filter by date range, like here.

bdfoster commented 8 years ago

Do not store a date as a number. Store it as a Date object:

From the documentation: var doc = { hello: 'world' , n: 5 , today: new Date() , nedbIsAwesome: true , notthere: null , notToBeSaved: undefined // Will not be saved , fruits: [ 'apple', 'orange', 'pear' ] , infos: { name: 'nedb' } };

That Date object works just like a JavaScript Date object (because it is).

There are 4 ways of initiating a date:

new Date() new Date(milliseconds) new Date(dateString) new Date(year, month, day, hours, minutes, seconds, milliseconds)

http://www.w3schools.com/js/js_dates.asp On Mar 30, 2016 2:35 PM, "Damir Mustafin" notifications@github.com wrote:

Try to store birthday's timestamp as a number. E.g.:

{ "name" : "Alan Pachuau", "birthday" : 823631400000 }, "_id" : "NmkakE7aHmJ19Xv5"}

This way, you can also filter by date range, like here http://stackoverflow.com/a/2943685.

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/louischatriot/nedb/issues/383#issuecomment-203570586

frenchbread commented 8 years ago

@bdfoster Will the filtering work with Date() object approach just like in MondoDB (using $lte and $gte )?

bdfoster commented 8 years ago

I'd imagine it would, but I'm not in a position where I can test easily. He doesn't need to use a filter though. On Mar 31, 2016 7:40 AM, "Damir Mustafin" notifications@github.com wrote:

@bdfoster https://github.com/bdfoster Will the filtering work with Date() object approach just like in MondoDB (using $lte and $gte )?

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/louischatriot/nedb/issues/383#issuecomment-203891701

frenchbread commented 8 years ago

@bdfoster Yep, I was asking for myself since didn't want to open separate issue.

bdfoster commented 8 years ago

Your referenced link says the same thing, to use ISODate to store a date. ISODate is a wrapper over the Date object. On Mar 31, 2016 7:55 AM, "Damir Mustafin" notifications@github.com wrote:

@bdfoster https://github.com/bdfoster Yep, I was asking for myself since didn't want to create separate issue.

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/louischatriot/nedb/issues/383#issuecomment-203896566

frenchbread commented 8 years ago

@bdfoster I needed to use filtering to fetch documents in specific range of dates. Date() obj didn't work "out of the box". And for now I've decided to go with unix timestap. Even though I'm gonna try it out with ISODate again.

bdfoster commented 8 years ago

You may not doing it correctly, but remember you can get epoch out of a Date object. If you're struggling, open another issue and post what you've done. On Mar 31, 2016 8:06 AM, "Damir Mustafin" notifications@github.com wrote:

@bdfoster https://github.com/bdfoster I needed to use filtering to fetch documents in specific range of dates. Date() obj didn't work "out of the box". And for now I've decided to go with unix timestap. Even though I'm gonna try it out with ISODate again.

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/louischatriot/nedb/issues/383#issuecomment-203901769

frenchbread commented 8 years ago

Filtering by ISODate works well. The issue was that I've been storing moment() object in the db instead of ISODate. moment.toDate() // converts to ISO Date did the thing.

alanpachuau commented 8 years ago

Thank you all for your response. @frenchbread so are you saying $lte, $gte works with the ISO Date?

frenchbread commented 8 years ago

@alanpachuau Yep.

FossPrime commented 6 years ago

Runkit confirming creating and finding date with $lt, $gt, etc filters works without issue: https://runkit.com/hesygolu/5ae24c7e67538600120112b0

Should probably close this.