node.js - Issue selecting collection properties using Mongoose mixed schema -
i'm working on project involves creating pages external source. since these external sources variable in nature, cannot strictly define schema use. did research , learned mongoose's mixed schema type , created following model:
var pagetemplate = new mongoose.schema( { }, { strict: false } );
during import process, perform upsert operation update existing entries , insert new ones:
pagetemplate .findoneandupdate( { slug: page.slug }, page, { upsert: true } );
everything works great far. problem arises when later try fetch entries. when console.log
entire object, expect--the entire object. reason, when go access single property, undefined
.
pagetemplate.findone( { slug: slug }, function(err, page) { console.log(page); // prints entire object console.log(page.slug); // undefined } )
have misunderstood how mixed schemas supposed behave? there commit operation need perform? example, simple insert do:
var page = new pagetemplate(data); page.save();
any nudges in right direction appreciated.
note: snippets have been simplified demonstrate how i've set project. if more context required, please let me know , i'll update question.
edit: may noteworthy add case properties not explicitly defined in schema. if define schema fields explicitly defined, can access them correctly seems defeat purpose of mixed type schema:
var pagetemplate = new mongoose.schema( { slug: string }, { strict: false } );
the object you're getting pagetemplate.findone
mongoose document, expose fields you've defined in schema.
you can call page.toobject()
convert document raw javascript object equivalent in order make of freeform schema fields accessible.
also, resolve of mystery, when console.log
ing mongoose document, .tostring()
method being invoked, outputting raw format.
Comments
Post a Comment