node.js - Defining a map with ObjectId key and array of strings as value in mongoose schema -
i'm facing problem while creating mongoose schema db. want create map objectid key , array of string values value. closest can is:
var schema = new schema({ map: [{myid: {type:mongoose.schema.types.objectid, ref: 'myothercollection'}, values: [string]}] });
but somehow not working me. when perform update {upsert: true}, not correctly populating key: value in map. in fact, i'm not sure if have declared schema correctly.
can tell me if schema correct ? also, how can perform update {upsert: true} schema?
also, if above not correct , can;t achieved how can model requirement other way. use case want keep list of values given objectid. don't want duplicates entries same key, that's why picked map.
please suggest if approach correct or should modelled other way?
update:
based on answer @phillee , this, i'm wondering can modify schema mentioned in accepted answer of mentioned thread this:
{ "_id" : objectid("4f9519d6684c8b1c9e72e367"), ... // other fields "myid" : { "4f9519d6684c8b1c9e73e367" : ["a","b","c"], "4f9519d6684c8b1c9e73e369" : ["a","b"] } }
schema like:
var schema = new schema({ myid: {string: [string]} });
if yes, how can change { upsert:true } condition accordingly ? also, complexity wise more simpler/complex compared original schema mentioned in thread?
i'd suggest changing schema have 1 entry per myid,
var schema = new schema({ myid : {type:mongoose.schema.types.objectid, ref: 'myothercollection'}, values : [string] })
if want update/upsert values,
model.update({ myid : myid }, { $set : { values : newvalues } }, { upsert : true })
Comments
Post a Comment