Skip to content

srcagency/date-to-object-id-hex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

date-to-object-id-hex

Convert a Date object to a MongoDB ObjectId hex string.

This is useful for searching documents in a MongoDb collection by creation date without having a dedicated field and index to do so.

This package does not have any dependencies and is just a few lines of code (in contrast to mongodb) making it perfect for the frontend, e.g. when dealing with APIs using Object Ids for pagination.

Examples

var toHex = require('date-to-object-id-hex');

var hex = toHex(new Date('2016-01-01'));	// 5685c1800000000000000000

// in Node.js, find all documents since 2016
collection.find({ _id: { $gt: ObjectId(hex) } });

About

Create a MongoDB Object Id hex string from a Date

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published