0

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.

2
  • I think you can just assign the string value of the ObjectId and it'll turn it in to a proper id. Such as article.tags = "553ecd7c0942f211d4420b36" Commented Nov 1, 2015 at 2:21
  • I'm not sure about what you're trying to achieve, are you trying to set a single ObjectId as reference for a document's property? Commented Nov 1, 2015 at 2:46

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!! var ObjectId = require('mongodb').ObjectId; !

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.