node.js - Confused about Mongoose/Mongo Terminology. Are Sub-Docs/Embedded-Docs also Collections? -
if have following mongoose models:
// child.model.js var mongoose = require('mongoose'), schema = mongoose.schema, childschema = new schema({ name: string, age: number }, {collection: 'children'}); module.exports = mongoose.model('child', childschema); //parent.model.js var mongoose = require('mongoose'), child = require('./child.model.js'), schema = mongoose.schema, parentschema = new schema({ name: string, children: [child.schema] }); module.exports = mongoose.model('parent', parentschema);
would mean have 2 collections, 1 called 'parents' , 1 called 'children'?
as understand above code creates nested document structure whereby child
objects exist within collection of parent
documents. however, i'm getting confused {collection: 'name'}
option can pass schema
constructor. option ignored when creating sub-documents this?
there 2 kinds of subdocs - embedded , referenced. mongoose-level classification. @ mongodb level it's collections , documents.
the difference between embedded , referenced docs in mongoose former akin having child schema "embedded" in parent. i.e. far mongodb concerned (parent) 1 big document.
whereas in referenced docs parent document stores child document's objectid, i.e. child document "referenced", , it's left "populate" entire document.
what you're using children: [child.schema]
syntax of embedded document.
would mean have 2 collections, 1 called 'parents' , 1 called 'children'?
so you'll have 1 collection in mongodb.
however, i'm getting confused {collection: 'name'} option can pass schema constructor. option ignored when creating sub-documents this?
that option if create model schema, uses name provided instead of automatically inferring.
Comments
Post a Comment