mongoose schema field associate array -
my schema looks like:
var article = new mongoose.schema({ sentencearray: {} });
the purpose of having sentenceaudioarray: {}
can add {sentid,senttext}
tuples article. can achieved through standard array ( sentencearray: []
), retrieval not o(1) (ie, id know sentid, cannot call sentencearray[sentid]
retrieve).
in createarticle methods, have
newarticle.sentencearray = {};
this doesnt seems create field in mongo. when @ database, field sentencearray not created. consequently, when
anarticle.sentencearray[sentid] = "some sentence"
i error sentencearray undefined.
any suggestions on how use associative array within mongoose schema.
edit: please note objective able add multiple sentences sentencearray. expect call
anarticle.sentencearray["firstsent"] = "some sentence"; anarticle.sentencearray["secondsent"] = "another sentence"; anarticle.sentencearray["thirdsent"] = "yet sentence";
and later on, call
console.log(anarticle.sentencearray["thirdsent"])
you should provide default:
var articleschema = new mongoose.schema({ sentencearray: { type : mongoose.schema.types.mixed, default : {} } });
the mixed
type tells mongoose sentencearray
(which object, not array) "can hold anything" property. there things aware of using type, should read documentation.
providing default not make mongoose write empty values of property database, make sure sentencearray
exists when instantiate document or retrieve 1 database.
Comments
Post a Comment