Convert string to ObjectID in MongoDB
Asked Answered
I

10

66

I am developing an API using Codeigniter and MongoDB. In some parts of the database I have saved the ID of an image in ObjectID format instead of a string. Now I got an ID in string format and I need to query the database using it.

How can I "convert" a string to an ObjectID so that I can do the query?

From this:

34234234234234234234

To this:

ObjectID("34234234234234234234")
Innermost answered 19/10, 2011 at 17:49 Comment(1)
MongoId is not a valid type in the Mongo REPLNationwide
H
65

Using mongoose:

var mongoose = require('mongoose');
var objectId = mongoose.Types.ObjectId('569ed8269353e9f4c51617aa');

Using native driver (https://mcmap.net/q/294250/-how-to-convert-a-string-to-objectid-in-nodejs-mongodb-native-driver)

var ObjectId = require('mongodb').ObjectId;
doc._id = new ObjectId(doc._id); // wrap in ObjectID
Howlond answered 20/1, 2016 at 1:5 Comment(2)
ObjectID should be ObjectId when using native driver.Deepsea
Update, we need to add new keyword before creating object. var objectId = new mongoose.Types.ObjectId('569ed8269353e9f4c51617aa');Demetrademetre
L
49

You just need to require the ObjectId function from your mongo.

ObjectId = require('mongodb').ObjectID;

Then you can use it like that:

ObjectId("34234234234234234234")
Logan answered 14/1, 2016 at 11:18 Comment(2)
this answer really helped!Planogamete
I'm getting: "Error: ReferenceError: require is not defined". What does this mean?Mall
C
7

If you are using Meteor

var id = new Mongo.ObjectID("34234234234234234234");
Choragus answered 27/9, 2016 at 14:35 Comment(0)
Q
4

you can now convert string to objectId on mongodb 4.0 and above. there is new feature to convert from string id to objectId

here you can see the documentation $toObjectId

Quisling answered 25/1, 2019 at 6:49 Comment(0)
P
2

If you are in an aggregation you can use the following :

{
   $toObjectId: <expression>
}

Which is a shortcut for :

{ $convert: { input: <expression>, to: "objectId" } }

From mongodb documentation

Psalterium answered 12/7, 2023 at 12:45 Comment(0)
P
1

Just try to use Types.ObjectId:

import { Types } from "mongoose";

const theId = new Types.ObjectId('661547e7ad2c840e4e872d44' as string);
Pages answered 9/4, 2024 at 18:11 Comment(0)
A
0

(Extracted from the question)


I found a solution. Just do this:

new MongoId('34234234234234234234');
Abohm answered 19/10, 2011 at 17:49 Comment(0)
N
0

or better use

var mongodb = require(‘mongodb’); //this might have been defined at the beginning of your code.
//now use it
query = {_id:mongodb.ObjectId('569ed8269353e9f4c51617aa')};

and the rest is the same.

Norland answered 10/7, 2018 at 21:29 Comment(0)
T
-1
import { ObjectId } from 'mongodb'
const query  = {"_id":ObjectId(req.params.productId)}
Transparent answered 20/9, 2022 at 23:9 Comment(0)
T
-4

mongo DB version v3.6.3

how to save string input to MongoDB ObjectId on express controller code

const mongoose = require('mongoose');
var ObjectId = require('mongodb').ObjectId;
user: mongoose.Types.ObjectId(req.body.user)
console.log(user, typeof(user))
Tales answered 14/7, 2020 at 14:29 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.