我有一个结构是这样的骨架,因为一个ui
可能会有多个type
:
const uiSchema = new Schema({
uId: {
type: ObjectId,
ref: 'User'
},
name: {
type: String
},
title: {
type: String
},
pictureUrl: {
type: Array
},
createDate: {
type: String
},
updateDate: {
type: String
},
type: [{
type: ObjectId,
ref: 'Type'
}],
});
然后type
的骨架是这样的:
const typeSchema = new Schema({
name: {
type: String
},
createDate: {
type: String
},
updateDate: {
type: String
},
uId: {
type: ObjectId,
ref: 'User'
}
});
那我在往 ui
中插入数据的时候该怎么写呢,找了好久,一脸懵逼。
各位大神给点思路。
如果我理解您的想法正确的话,供参考:
1、Mongoose的schema的ref,主要用于方便查询中多个collection之间的关联:populate;
2、插入数据的时候,需要自己处理插入的逻辑,分别插入不同的collection,并保持不同的collection中的ref的ObjectId的一致
供参考。
Love MongoDB! Have fun!
MongoDB在线讲座系列19- MongoDB 10步构建单一视图
就在4月19日,请戳:>---<