How can I generate an ObjectId with mongoose?

171,151

Solution 1

You can find the ObjectId constructor on require('mongoose').Types. Here is an example:

var mongoose = require('mongoose');
var id = mongoose.Types.ObjectId();

id is a newly generated ObjectId.


Note: As Joshua Sherman points out, with Mongoose 6 you must prefix the call with new:

var id = new mongoose.Types.ObjectId();

You can read more about the Types object at Mongoose#Types documentation.

Solution 2

You can create a new MongoDB ObjectId like this using mongoose:

var mongoose = require('mongoose');
var newId = new mongoose.mongo.ObjectId('56cb91bdc3464f14678934ca');
// or leave the id string blank to generate an id with a new hex identifier
var newId2 = new mongoose.mongo.ObjectId();

Solution 3

I needed to generate mongodb ids on client side.

After digging into the mongodb source code i found they generate ObjectIDs using npm bson lib.

If ever you need only to generate an ObjectID without installing the whole mongodb / mongoose package, you can import the lighter bson library :

const bson = require('bson');
new bson.ObjectId(); // 5cabe64dcf0d4447fa60f5e2

Note: There is also an npm project named bson-objectid being even lighter

Solution 4

With ES6 syntax

import mongoose from "mongoose";

// Generate a new new ObjectId
const newId2 = new mongoose.Types.ObjectId();
// Convert string to ObjectId
const newId = new mongoose.Types.ObjectId('56cb91bdc3464f14678934ca');
Share:
171,151

Related videos on Youtube

Dmitry Minkovsky
Author by

Dmitry Minkovsky

Updated on September 10, 2021

Comments

  • Dmitry Minkovsky
    Dmitry Minkovsky over 2 years

    I'd like to generate a MongoDB ObjectId with Mongoose. Is there a way to access the ObjectId constructor from Mongoose?

    • This question is about generating a new ObjectId from scratch. The generated ID is a brand new universally unique ID.

    • Another question asks about creating an ObjectId from an existing string representation. In this case, you already have a string representation of an ID—it may or may not be universally unique—and you are parsing it into an ObjectId.

  • R01010010
    R01010010 almost 8 years
    how can you be sure that this is really unique? does mongoose any check before?
  • Dmitry Minkovsky
    Dmitry Minkovsky almost 8 years
    @R01010010 check out how these are generated (Google). There's no check but it's probabilistic
  • Raja
    Raja almost 8 years
    If you generate 16777216 within one second on the same machine, then you will probably get a duplicate. :)
  • Evading Shadows
    Evading Shadows almost 4 years
    Correction: If you generate 16777216 within one millisecond on the same machine, then you will probably get a duplicate. 😁
  • Saahithyan Vigneswaran
    Saahithyan Vigneswaran about 3 years
    @joeytwiddle what is this number 16777216 ?
  • Dmitry Minkovsky
    Dmitry Minkovsky about 3 years
    @SaahithyanVigneswaran it's 2^24, supposing you are using 24 bits for the random portion of the ID
  • Raja
    Raja about 3 years
    How are MongoDB's ObjectIds generated? The local counter is three bytes, or six hex chars. We can calculate: 256 ** 3 === 16 ** 6 === 16777216
  • Joshua Sherman
    Joshua Sherman over 2 years
    looks like with mongoose 6 you have to add new new mongoose.Types.ObjectId()
  • Dmitry Minkovsky
    Dmitry Minkovsky over 2 years
    Thank you @JoshuaSherman. I updated the answer.
  • MartianMartian
    MartianMartian about 2 years
    what's the diff between mongoose.Type and mongoose.Schema.Types ?
  • stefano
    stefano about 2 years
    mongoose.Schema.Types will be used to within your schema when you want to reference between collections. To create a single objectId within your code use simply mongoose.Types