Justin Young
Justin Young

Reputation: 2463

Saving subdoc ref and parent document simultaneously

I have a users model with a ref to company. My data is as follows:

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

Is it possible to save my data as a single call or would I need to save my company model first, then save my user afterward?

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

Upvotes: 1

Views: 90

Answers (1)

Jeffrey A. Gochin
Jeffrey A. Gochin

Reputation: 964

In a NoSQL database everything is document oriented so you wouldn't typically do what you are trying to do. You will need to manage the "foreign key" relationship yourself, in code.

Upvotes: 1

Related Questions