208

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.

0

5 Answers 5

427

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.

8
  • 4
    how can you be sure that this is really unique? does mongoose any check before?
    – R01010010
    Jun 6, 2016 at 9:49
  • 1
    @R01010010 check out how these are generated (Google). There's no check but it's probabilistic Jun 6, 2016 at 13:13
  • 25
    If you generate 16777216 within one second on the same machine, then you will probably get a duplicate. :) Jun 20, 2016 at 2:58
  • 3
    Correction: If you generate 16777216 within one millisecond on the same machine, then you will probably get a duplicate. 😁 Jul 11, 2020 at 11:13
  • 8
    looks like with mongoose 6 you have to add new new mongoose.Types.ObjectId() Sep 9, 2021 at 12:05
65

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();
32

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

29

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');
3
  • 2
    what's the diff between mongoose.Type and mongoose.Schema.Types ? Feb 21, 2022 at 11:18
  • 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
    – stefano
    Mar 8, 2022 at 13:43
  • @stefano, mongoose.Schema.Types.ObjectId and mongoose.Types.ObjectId, is the exactly same
    – Normal
    Sep 27, 2022 at 17:30
0

The import is mongo from mongoose, the node-mongodb-native driver. If you open the package you can verify !

import { mongo } from 'mongoose'
const newId = new mongo.ObjectId()
0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.