I have a 3 models. ( Collection-Article-Author ) I must connect all to each other with _id in mongodb. But i dont know how can i make it. For Example :I will have 3 collections. In article must have a field like collectionID and it must be same with belongs to Collection _id. How can i do it ? Thanks for all helps!
Let's say you got an article schema like this:
mongoose.model('Article', {
name: { type: String },
publishedAt: { type: String },
authorId: {type: db.Schema.Types.ObjectId, ref: 'Author'}
}
In your model define a new field as authorId
like this:
mongoose.model('Article', {
name: { type: String },
publishedAt: { type: String },
authorId: {type: db.Schema.Types.ObjectId, ref: 'Author'} //New field
}
And in the code:
const article = new Article({
authorId: mongoose.Types.ObjectId(authorId),
name: 'foo',
publishedAt: '2021-07-02'
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.