OOPS! Edit:
I completely misunderstand your question. So you want to save one subdocument? Then why do you have a property called children..
You can use:
var ParentSchema = new schema({
name : String,
child: Object
});
john.child = new Child({name: 'kiki'});
john.save();
, mongoose . , ? . , .
:
, , ( ?).
, : parents children (= Parent & Child). children parents. Parent , children.
, (, methods statics) , :
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/myDB');
var parentSchema = new mongoose.Schema({
name : String,
children : [{type:mongoose.Schema.Types.ObjectId, ref:'Child'}]
});
var childSchema = new mongoose.Schema({
name : String,
parent : {type:mongoose.Schema.Types.ObjectId, ref:'Parent'}
});
var Child = mongoose.model('Child', childSchema),
Parent = mongoose.model('Parent', parentSchema);
var john = new Parent({name:'John'});
var child1 = new Child({name:'Mimi', parent:john._id}),
child2 = new Child({name:'Kiki', parent:john._id});
john.children.push(child1);
john.children.push(child2);
john.save();
child1.save();
child2.save();
:
/**
Children:
[ { name: 'Mimi',
parent: 537258f63eb92b3201b65e56,
_id: 537258f63eb92b3201b65e57,
__v: 0 },
{ name: 'Kiki',
parent: 537258f63eb92b3201b65e56,
_id: 537258f63eb92b3201b65e58,
__v: 0 } ]
Parent:
[ { name: 'John',
_id: 537258f63eb92b3201b65e56,
__v: 0,
children: [ 537258f63eb92b3201b65e57, 537258f63eb92b3201b65e58 ] } ]
*/
parents: addChild(child, callback), ( javaísh).
:
parentSchema.statics.addChild = function(child, callback) {
}
parentModel.addChild(new Child.etc..);
, (: