How can I generate an ObjectId with mongoose?
Asked Answered
Z

7

217

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.

Zia answered 27/7, 2013 at 16:2 Comment(0)
Z
439

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.

Zia answered 27/7, 2013 at 16:2 Comment(8)
how can you be sure that this is really unique? does mongoose any check before?Trumpet
@Trumpet check out how these are generated (Google). There's no check but it's probabilisticZia
If you generate 16777216 within one second on the same machine, then you will probably get a duplicate. :)Redpoll
Correction: If you generate 16777216 within one millisecond on the same machine, then you will probably get a duplicate. 😁Eusebioeusebius
@Redpoll what is this number 16777216 ?Affluent
@SaahithyanVigneswaran it's 2^24, supposing you are using 24 bits for the random portion of the IDZia
How are MongoDB's ObjectIds generated? The local counter is three bytes, or six hex chars. We can calculate: 256 ** 3 === 16 ** 6 === 16777216Redpoll
looks like with mongoose 6 you have to add new new mongoose.Types.ObjectId()Hersch
A
69

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();
Azikiwe answered 22/2, 2016 at 23:33 Comment(1)
mongoose.mongo?Baptiste
S
33

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

Sacrum answered 9/4, 2019 at 0:30 Comment(1)
The size of "unpacked" bson is almost as heavy as mongodb.Tocsin
T
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');
Trek answered 26/7, 2019 at 6:39 Comment(3)
what's the diff between mongoose.Type and mongoose.Schema.Types ?Galeiform
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.TypesGelasias
@stefano, mongoose.Schema.Types.ObjectId and mongoose.Types.ObjectId, is the exactly sameAsyndeton
E
1

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()
Eliaeliades answered 19/3, 2023 at 21:37 Comment(0)
O
0

The code that worked for me in mongoose v8 (+ typescript) was this:

const id = new mongoose.Types.ObjectId().toString()
Orlosky answered 14/1 at 4:27 Comment(0)
T
0
const mongoose = require('mongoose');
const objectId = new mongoose.Types.ObjectId();

console.log(objectId);
Tangled answered 14/1 at 5:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.