nested object properties must be called 'meta'?

619 views
Skip to first unread message

Tim Oxley

unread,
Nov 5, 2011, 12:26:59 AM11/5/11
to mongoo...@googlegroups.com
Apparently you can embed additional keys inside a key, but I can't find any info about it, it's only appearance is in examples on the two pages referenced below, it's always in a property called 'meta'.

Is this only available for the property 'meta'?
Does it also mean I can't call my nested attributes things like 'type' or 'required'?
Are there any other caveats, eg I have to call markModified when I set any 'meta' info?


var BlogPost = new Schema({
    author    : ObjectId
  , title     : String
  , body      : String
  , date      : Date
  , comments  : [Comments]
  , meta      : {
        votes : Number
      , favs  : Number
    }
});

From: http://mongoosejs.com/docs/embedded-documents.html


var Person = new Schema({ title : { type: String, required: true } , age : { type: Number, min: 5, max: 20 } , meta : { likes : [String] , birth : { type: Date, default: Date.now } } });



Aaron Heckmann

unread,
Nov 5, 2011, 4:51:33 PM11/5/11
to mongoo...@googlegroups.com
You are free to use any property name, not just "meta". 

Re: type/required - Good question, type is reserved but there is a work-around. To use it you must also specify your type with object syntax:

nested: { type: String } // bad
nested: {  type: { type: String }} // good

These are not Mixed types so there is no need to call markModified.

--
http://mongoosejs.com
http://github.com/learnboost/mongoose
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en



--
Aaron


Message has been deleted

Tim Oxley

unread,
Nov 5, 2011, 8:47:04 PM11/5/11
to mongoo...@googlegroups.com
Thanks Aaron, turns out there's a little documentation for nested type/required on the home page, but I guess I missed it since it wasn't with the other schema info on the model/schema-types pages. 


so in the other thread I referenced, you state: 
"Using a plain object will create an array of Mixed types which means you'll have to babysit Mongoose and call markModified when you update any of these objects."

So the rule is, you can nest properties, but you can't have an array of nested properties without a schema? 

e.g. I can use

var Image = new Schema({
    caption   : String
  , dimensions: {
        width : Number
      , height  : Number
    }
});

But not:
var Image = new Schema({
    caption   : String
  , versions  : [{
        name  : String
        width : Number
      , height: Number
    }]
});

Without creating a Schema/using markModified for my 'versions'?

Why is that? Something about an array means you can't listen for changes inside it without wrapping it with schema? Just trying to understand how mongoose works a little better.

Aaron Heckmann

unread,
Nov 7, 2011, 10:32:26 AM11/7/11
to mongoo...@googlegroups.com
On Sat, Nov 5, 2011 at 8:47 PM, Tim Oxley <sec...@gmail.com> wrote:
Why is that? Something about an array means you can't listen for changes inside it without wrapping it with schema?

Nothing about the array really, we just don't assume its a document. No doubt arrays are inconsistent with top-level declarations with respect to Mixed.

thing: {} // mixed
thing: Mixed // mixed
thing: { type: Mixed } // mixed
thing: [{}] // array of mixed
thing: [] // array of mixed
thing: [Mixed] // array of mixed
thing: [{ type: Mixed }] // array of mixed
thing: { hi: { there: String }} // string
thing: [{ hi: { there: String }}] // array of mixed
thing: [new Schema({ hi: { there: String }})] // array of docs

I would kinda like Mongoose to assume its a doc, it seems more natural but if we change it we will break backwards compatibility so it would have to wait til the next major release.

--
Aaron


Jason Citron

unread,
Dec 5, 2011, 10:47:11 PM12/5/11
to mongoo...@googlegroups.com
Would probably be a good idea to call this out in big bright letters somewhere in the docs. This is ultra sad nuance magic. Burying such an obvious use case on the schema types page is bad form. I blew 3 days wrestling with this before I managed to find the discussions. (Not knowing what to search for was a big part of the problem too - had I known I was creating a mixed type inside an array it would have helped. Coming from C++ & Ruby I assumed it was a "hash" or an "array" with keys)

Anyway - Mongoose is cool. Just needs some more polishing :-)

Aaron Heckmann

unread,
Dec 6, 2011, 8:22:51 AM12/6/11
to mongoo...@googlegroups.com
On Mon, Dec 5, 2011 at 10:47 PM, Jason Citron <jason....@gmail.com> wrote:
Would probably be a good idea to call this out in big bright letters somewhere in the docs. This is ultra sad nuance magic. Burying such an obvious use case on the schema types page is bad form.

Yes the docs, like everything else in this project, could use more love. Are you volunteering to help improve the them? :)

Its pretty easy to do. Just fork the project, `git checkout gh-pages`, and edit the markdown files in the docs/ directory. Or even edit the markdown files directly in githubs editor.

--
Aaron


Reply all
Reply to author
Forward
0 new messages