Why do new documents in mongo have an object, not an ObjectId?

When inserting new documents into mongodb, the identifiers are not like ObjectId, and instead they look like an object.

"_id" : { "_bsontype" : "ObjectID", "id" : "U\u0013[-Ф~\u001d$©t", "generationTime" : 1.43439e+09 } 

Expected Type:

 "_id" : ObjectId("55107edd8e21f20000fd79a6") 

My version is mongodb 3.0.3 and it is pretty much code and schema

 var Script = { run: function() { return CourseModel.findQ() .then(function(courses){ return courses.map(worker); }).catch(function(error){ console.log(error); }); } }; function worker(course){ var category = { name: course.name, displayOrder: 0 }; return CategoryModel.createQ(category).then(function() { course.set('name', undefined); return course.saveQ(); }); } module.exports = Script; var CategorySchema = new Schema({ name: { type: String, required: true, unique: true }, active: { type: Boolean, default: true }, displayOrder: Number, updateDate: Date, subcategories: [{ type: Schema.Types.ObjectId, ref: 'subcategories' }] }); 
+5
source share
3 answers

Erroneous ObjectIds are caused by a conflict with the mongoose version that mongoose-q uses. You will need to upgrade mongoose-q to version 0.1.0. I used 0.0.17 earlier and saw exactly the same behavior as here.

+1
source

Here is what ObjectID is. It is simply an object that contains these properties.

http://docs.mongodb.org/manual/reference/object-id/

ObjectId is a 12-byte BSON type built using:

  • A 4-byte value representing seconds since the Unix era,
  • 3-byte machine identifier
  • double-byte process identifier and
  • 3-byte counter, starting with a random value.
 { "_bsontype" : "ObjectID", "id" : "U\u0013[-Ф~\u001d$©t", "generationTime" : 1.43439e+09 } 

U\u0013[-Ф~\u001d$©t is a 12-character binary string that is converted to a signed hexadecimal string char ( 55107edd8e21f20000fd79a6 ) when the whole object is represented as a text value (i.e. .toString function is called)

In Mongoose, documents also have a .id getter, which gives you 24 char hex as a string value.

+1
source

I had the same problem: ObjectID did not store the hex value

This is definitely a problem with the environments and something strange with installing MongoDB brew. I found that uninstalling from brew and reinstalling it manually solved my problem. http://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/

So far, I haven’t understood from the code / technical point of view why it returns a 12-byte BSON ObjectID and not a hexadecimal ObjectID ... removing MongoDB from brew and reinstalling it manually solves the problem.

0
source

All Articles