我的mongoDB中有一个复杂的架构
var bookSchema = mongoose.Schema({
_creator: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
content: {
page: [
{
img: String,
frame: [
{
cx: Number,
cy: Number,
scale: Number,
rotation: Number,
cropping: {
x: [Number],
y: [Number],
visible: Boolean,
color: String,
shape: String
},
transition: {
easing: String,
time: Number,
type: String
},
requireUserInput: Boolean
}
]
}
]
},
views: Number,
name: String,
genre: String,
Description: String,
hasBecomicsMode: Boolean,
dateOfCreation: Date,
published: Boolean
})
渲染时,会向每个字段添加一个_id,如下所示:
book = {
_creator: "some user ID",
content: {
page: [
{
_id : "an ID is automatically added",
img: "a path",
frame: [
{
_id: "an ID is automatically added",
cx: Number,
cy: Number,
//etc...
}
]
}
]
},
//etc...
}
这使得无法复制字段“content”,因为_ids发生了冲突。
有没有办法制作这样的东西:
newBook = existingBook.removeIDs() //kinda thing...
答案 0 :(得分:0)
定义嵌入式架构时,可以将选项指定为第二个参数,您可以在其中阻止文档获取Mongoose创建的_id
(父文档在MongoDB时仍会创建_id
插入)。因此,您可以按以下方式定义模式:
var frameSchema = new Schema(
{
cx: Number,
cy: Number,
scale: Number,
rotation: Number,
cropping: {
x: [Number],
y: [Number],
visible: Boolean,
color: String,
shape: String
},
transition: {
easing: String,
time: Number,
type: String
},
requireUserInput: Boolean
},
{ _id: false }
);
var pageSchema = new Schema(
{
img: String,
frame: [frameSchema]
},
{ _id: false }
);
var bookSchema = mongoose.Schema({
_creator: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
content: {
page: [pageSchema]
},
views: Number,
name: String,
genre: String,
Description: String,
hasBecomicsMode: Boolean,
dateOfCreation: Date,
published: Boolean
})
有关详细信息,请参阅 documentation 。