Mongoose: how to insert one subdocument - not an array

In the mongoose, you can declare a schema for the subdocument:

        var childSchema = new mongoose.Schema({ name: String, age: String });
        var parentSchema = new mongoose.Schema({
            children: [childSchema]
        });
        var Parent = mongoose.model('Parent', parentSchema);
        var Child = mongoose.model('Child', childSchema);

        var child = new Child({name:'Joe', age: '21'});
        var parent = new Parent();

        parent.children = [child];

        parent.save(function(err, saved) {
            if(err) console.error(err);
        });

It seems that the type of subdocument should be an array. I need to save the subdocument as a single instance, not an array, that is:

        var childSchema = new mongoose.Schema({ name: String, age: String });
        var parentSchema = new mongoose.Schema({
            children: childSchema // not an array
        });
        var Parent = mongoose.model('Parent', parentSchema);
        var Child = mongoose.model('Child', childSchema);

        var child = new Child({name:'Joe', age: '21'});
        var parent = new Parent();

        parent.children = child; // not an array

        parent.save(function(err, saved) {
            if(err) console.error(err);
        });

So, not an array or ref, but only one instance of the instance. Is it possible? If not, I should use:

var childInstance = child.toObject();
+4
source share
3 answers

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
});

// etc.

john.child = new Child({name: 'kiki'});
// which is actually the same as: john.child = {name:'kiki'};
john.save();

, mongoose . , ? . , .

:

, , ( ?).

, : parents children (= Parent & Child). children parents. Parent , children.

, (, methods statics) , :

//  get mongoose.
var mongoose = require('mongoose');

//  connect to your local pc on database myDB.
mongoose.connect('mongodb://localhost:27017/myDB');

//  parent schema.
var parentSchema = new mongoose.Schema({
  name     : String,
  //  the ObjectId of the children links the two schema's.
  children   : [{type:mongoose.Schema.Types.ObjectId, ref:'Child'}]
});

//  child schema.
var childSchema = new mongoose.Schema({
  name   : String,
  //  the parent ObjectId links to the owner.
  parent : {type:mongoose.Schema.Types.ObjectId, ref:'Parent'}
});

//  model the schema's.
var Child   = mongoose.model('Child', childSchema),
    Parent  = mongoose.model('Parent', parentSchema);

//  create a 'parent'.
//  we are not assigning the children yet.
var john     = new Parent({name:'John'});

//  create two children and save them. Link them to john.
var child1   = new Child({name:'Mimi', parent:john._id}),
    child2   = new Child({name:'Kiki', parent:john._id});

//  push child to children property of john.
john.children.push(child1);
john.children.push(child2);

//  save everything.
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).

:

// add custom static method.
parentSchema.statics.addChild = function(child, callback) {
   // implementation.
}

// the way you call this method:
parentModel.addChild(new Child.etc..);

, (:

+4

. , . :

    var parentSchema = new mongoose.Schema({
        child: { 'name' : String, 'age' : Number }  // not an array, just a sub document
    });
    var Parent = mongoose.model('Parent', parentSchema);

    var parent = new Parent();
    parent.child.name = "Joe";
    parent.child.age  = 13;

    parent.save(function(err, saved) {
        if(err) console.error(err);
    });
+4

1 1. 'ref'

var personSchema = Schema({
  _id     : Number,
  name    : String,
  age     : Number,
  stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});

var storySchema = Schema({
  _creator : { type: Number, ref: 'Person' },
  title    : String,
  fans     : [{ type: Number, ref: 'Person' }]
});

: http://mongoosejs.com/docs/populate.html

0

Source: https://habr.com/ru/post/1540346/


All Articles