Push items into mongo array via mongoose

前端 未结 8 1577
小鲜肉
小鲜肉 2020-11-22 03:22

I\'ve scoured SO a good bit looking for the answer but I\'m sure that I\'m lost for the right words to describe what I\'m after.

Basically I have a mongodb collection

8条回答
  •  暖寄归人
    2020-11-22 04:05

    I ran into this issue as well. My fix was to create a child schema. See below for an example for your models.

    ---- Person model

    const mongoose = require('mongoose');
    const SingleFriend = require('./SingleFriend');
    const Schema   = mongoose.Schema;
    
    const productSchema = new Schema({
      friends    : [SingleFriend.schema]
    });
    
    module.exports = mongoose.model('Person', personSchema);
    

    ***Important: SingleFriend.schema -> make sure to use lowercase for schema

    --- Child schema

    const mongoose = require('mongoose');
    const Schema   = mongoose.Schema;
    
    const SingleFriendSchema = new Schema({
      Name: String
    });
    
    module.exports = mongoose.model('SingleFriend', SingleFriendSchema);
    

提交回复
热议问题