When inserting new documents in mongodb, ids don't look 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 mongodb version is 3.0.3 and this is pretty much the code and the 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'
}]
});
That is what an ObjectID is. It is simply an object that contains those properties.
http://docs.mongodb.org/manual/reference/object-id/
ObjectId is a 12-byte BSON type, constructed using:
- a 4-byte value representing the seconds since the Unix epoch,
- a 3-byte machine identifier,
- a 2-byte process id, and
- a 3-byte counter, starting with a random value.
{
"_bsontype" : "ObjectID",
"id" : "U\u0013[-Ф~\u001d$©t",
"generationTime" : 1.43439e+09
}
U\u0013[-Ф~\u001d$©t
is the 12 character binary string which gets converted to the familiar 24 char hex string (55107edd8e21f20000fd79a6
) when the object as a whole is represented as a text value (i.e. its .toString
function is invoked)
In Mongoose the documents also have a .id
getter which give you the 24 char hex as a string value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With