node.js - Mongoose.js: is it possible to change name of ObjectId? -
some question mongo objectid in mongoose
1) can objectid field named not _id? , how that? when in code:
myschema = new mongoose.schema({ id : mongoose.schema.objectid });
it changes nothing.
2) if have objectid field called _id possible return request name field (for example "id" - send on in web response);
3) , question understanding: why objectid _id field accessible through "id" property not "_id"?
thanks, alex
the "_id" element part of mongodb architecture guarantee every document in collection can uniquely identified. important if use sharding allow unique identifier across disparate machine. therefore design choice there no way ride of :)
the default value _id generated follows:
- timestamp
- hash of machine hostname
- pid of generating process
- increment
but can use whatever value want long unique.
if it's easier think _id of has there, don't care :) leave system auto generate , use own identifier.
so if still wanna create own "id" execute that:
db.myschema.ensureindex({"id": 1}, {"unique" : true})
but make sure unique , doesn't conflict api use.
2) rename on application side, before sending web response.
3) think because of api use. maybe author found more logical return id instead of _id ? never tried mongoose :)
Comments
Post a Comment