okel
okel

Reputation: 37

Manually insert ObjectId with Javascript

I got an object called "article" that I directly iinsert nto MongoDB. But I'm having issue with one of the sub-objects:

article.tags = ObjectId("' + tags + '");
//This doesn't work because: ObjectId is not defined
article.tags = "ObjectId("' + tags + '")"; 
//This works half-way because: ObjectId gets inserted as a custom value "ObjectId"
// inside commas, not as a true
// ObjectId, therefore my application won't interpret it.

I have thought of requiring some mongodb driver in the app, would it work? Is there any cleaner way to approach this?

Note: Tags are already defined and properly indexed in the database, and they must be inserted this way.

Upvotes: 0

Views: 620

Answers (1)

webdeb
webdeb

Reputation: 13211


Maybe it's just a typo: ObjectId !== ObjectID ???


Have you required ObjectID or only mongodb client?

var ObjectID = require('mongodb').ObjectID;

then you should do:

var someId = new ObjectID("ABCDEFABCDEFABCDEFABCDEF") // should be 24 byte long

Upvotes: 1

Related Questions