node.js - Weird mongoose behavior when viewing ObjectIds? -
when viewing sub-document robomongo see this:
"views" : [ objectid("53a478431275cf0f3d91e27d"), objectid("53a478431275cf0f3d91e27d") ] but when pull down object through mongoose node.js, see this:
views: [ { _bsontype: 'objectid', id: 't\u001aôj#Ü«m¢©Ö', viewdate: '2015-07-07t23:21:32.259z' } ] yes, schema little different, , i'm trying write script remediate data new format.
the schema
views: [{view:{type: schema.types.objectid, ref: 'users'},viewdate:{type: date, default: date.now}}], but
a) why view object messed in latter, and
b) how can see in robomongo? (answered. see edit)
edit: question b answered. if .lean() query, i'll able non-mongoose object , it'll how expect look. leaves question a
i managed reproduce this.
first, declared schema similar this:
views : { type : schema.types.objectid, ref : 'users' }you created , wrote documents database using schema.
then changed schema current:
views: [{ view : { type: schema.types.objectid, ref: 'users' }, viewdate : { type: date, default: date.now } }]using schema, reading documents wrote database using first schema.
those schema fundamentally different: first stored single objectid in database (the term "subdocument" bit confusing, because in mongoose, subdocuments documents stored parent document; method you're using called "population" in mongoose-speak), second schema makes views array of documents have 2 properties (view, stored objectid , viewdata date).
this confuses mongoose because tries apply second schema documents written using first schema, , because of that, it's showing internal representation of objectid object instead of stringified version of it.
this explains why .lean() shows correct results, because tells mongoose return raw documents (as stored in database) instead of trying convert them according schema.
Comments
Post a Comment