同时保存子文档引用和父文档

Saving subdoc ref and parent document simultaneously

我有一个引用公司的用户模型。我的数据如下:

{
"name":"justin", 
"notes":"hello notes",
"company":
    {"name":"acme inc"}
}

是否可以将我的数据保存为单个调用,或者我是否需要先保存我的公司模型,然后再保存我的用户?

var UserSchema = new Schema({
    name: {
        type: String,
        trim: true,
        default: ''
    },
    notes:{
        type:String
    },
    company: {
        type: Schema.ObjectId,
        ref: 'Company'
    })

在 NoSQL 数据库中,一切都是面向文档的,因此您通常不会做您想做的事情。您需要在代码中自行管理 "foreign key" 关系。