首页 > 解决方案 > 使用空子文档(数组)在猫鼬中插入文档,创建空文档?

问题描述

const mongoose = require('mongoose');

const orgSchema = new mongoose.Schema({
org_name : {
    type : String,
    required : true
},
admin : {
    type : String,
    required : true
},
branches : [{
    branch_name : {
        type : String,
        required :true
    },
    branch_admin : {
        type : String,
        required : true
    },
    branch_locations : [{
        _id : false,
        sub_branch_name : {
            type : String
        },
        sub_branch_admin : {
            type : String
        }
    }]
}],
plan : {
    type : mongoose.Schema.Types.ObjectId,
    ref : 'plan',
    required : true
},
status : {
    type : String,
    enum : ['ACTIVE', 'EXPIRED']
}
});

orgSchema.createIndex = ({
 org_name : 1
},{
unique : true
});

module.exports = mongoose.model('organizations', orgSchema);

//上面是架构

    {
"_id" : ObjectId("5bf91a1edc7ed22ca90d4624"),
"org_name" : "xxxxxxxxxxxxxxx",
"admin" : "sssssss",
"branches" : [ 
    {
        "_id" : ObjectId("5bf91a1edc7ed22ca90d4625"),
        "branch_name" : "ssssss",
        "branch_admin" : "ddd",
        "branch_locations" : [ 
            {}//automatically creates empty doc when no fields
        ]
    }
],
"plan" : ObjectId("5bf91a1edc7ed22ca90d4623"),
"status" : "ACTIVE",
"__v" : 0

当我尝试在子文档数组中插入一个包含空字段的文档时,它会在子文档数组中返回 {}。如何摆脱子文档中的新空文档。当子文档中没有要保存的字段时,我应该得到一个空数组...

标签: node.jsmongodbmongoosenosql

解决方案


推荐阅读