Socket
Book a DemoInstallSign in
Socket

moment-objectid

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

moment-objectid

Format a moment instance as ObjectID-string for use in MongoDB queries

0.0.1
latest
Source
npmnpm
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

moment-objectid

Format a moment instance as ObjectId-string for use in MongoDB queries.

As per spec an ObjectId contains a timestamp as the first eight bytes. Think of it this way: the _id field in MongoDB already contains a createdAt timestamp. Yay. And it's already indexed. Double yay!

This module extends the Moment prototype and adds a new toObjectId method. Note that it returns a string, not an instance of ObjectId (because this depends on the driver you use).

Installation

First do a npm install moment-objectid. Then require and execute it (the module returns a function).

//Patch the moment prototype.
require('moment-objectid')();

Note: moment-objectid does not list moment as dependency, because then it would only patch its own dependency and not necessarily the moment you use in your code.

Usage

Example using Mongoose (if you're not using Mongoose, you need to wrap the returned string in a ObjectId. Mongoose does that for us.).

var moment = require('moment');

//All posts from October 2012.
var query = {
	_id: {
		$gte: moment('2012-10-01').toObjectId(),
		$lte: moment('2012-10-01').endOf('month').toObjectId()
	}
};

BlogPost.find(query).exec(function(err, posts) {
	//Do stuff with the posts. Or don't. I'm not telling you how to live your life.
});

Keywords

moment

FAQs

Package last updated on 15 Dec 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.