猫鼬模式嵌入式文档默认 [英] Mongoose Schema Embedded Document Default
问题描述
我正在尝试将一个文档(因此是一个子文档)嵌入到Mongoose模式中.我一直遵循猫鼬文档建议的模式.我的架构如下所示.
I am trying embed a document, so I guess a subdocument, into a Mongoose schema. I have been following this pattern that the Mongoose documentation suggests. My schemas look like this.
var CategoriesSchema = new Schema({
name: { type: String, required: true, default: 'default' }
});
var PostSchmea = new Schema({
title: { type: String, required: true, default: 'default title' },
writtenBy: { type: ObjectId, required: true, default: '55878003ebf4b06110ef2ff8' },
publishedOn: { type: Date, default: Date.now },
updatedOn: Date,
published: { type: Boolean, required: true, default: false },
content: { type: String, required: true, default: 'empty' },
category: { type: [CategoriesSchema] },
tags: [String],
images: [String]
});
如您所见,我将CategoriesSchema嵌入到PostSchema中.我的问题是如何在PostSchema内为类别键设置默认值.我尝试了以下操作,并收到错误"TypeError:无法读取未定义的属性'$ __'":
As you can see I have the CategoriesSchema embedded into the PostSchema. My question is how do I set a default for the category key inside of the PostSchema. I tried the following and got the error "TypeError: Cannot read property '$__' of undefined":
var PostSchmea = new Schema({
title: { type: String, required: true, default: 'default title' },
writtenBy: { type: ObjectId, required: true, default: '55878003ebf4b06110ef2ff8' },
publishedOn: { type: Date, default: Date.now },
updatedOn: Date,
published: { type: Boolean, required: true, default: false },
content: { type: String, required: true, default: 'empty' },
category: { type: [CategoriesSchema], default: [CategoriesSchema] },
tags: [String],
images: [String]
});
我还尝试了以下操作,并收到错误"SyntaxError:意外令牌":
And I also tried the following and got the error "SyntaxError: Unexpected token":
var PostSchmea = new Schema({
title: { type: String, required: true, default: 'default title' },
writtenBy: { type: ObjectId, required: true, default: '55878003ebf4b06110ef2ff8' },
publishedOn: { type: Date, default: Date.now },
updatedOn: Date,
published: { type: Boolean, required: true, default: false },
content: { type: String, required: true, default: 'empty' },
category: { type: [CategoriesSchema], default: new CategoriesSchema(name: "default") },
tags: [String],
images: [String]
});
任何建议或文档将不胜感激.
Any suggestions or documentation would be greatly appreciated.
谢谢
推荐答案
我相信问题可能出在您的依赖项声明中.我使用您的代码声明了猫鼬模式,并且可以正常工作,但是您没有包括依赖项或创建方法,因此,让我向您展示如何创建记录,希望对您有所帮助.您也可以参考此Stack Overflow答案以获取ObjectId的依赖项声明:
I believe the problem might lie in your dependency declarations. I used your code to declare the mongoose schemas, and it worked correctly, however you did not include your dependencies, or your create methods, so let me show you how I created the record and I hope that helps. You can also reference this Stack Overflow answer for dependency declaration of the ObjectId: How to set ObjectId as a data type in mongoose
这是我的代码:
//contents of stackoverflow.js
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var CategoriesSchema = new Schema({
name: { type: String, required: true, default: 'default' }
});
var PostSchmea = new Schema({
title: { type: String, required: true, default: 'default title' },
writtenBy: { type: ObjectId, required: true, default: '55878003ebf4b06110ef2ff8' },
publishedOn: { type: Date, default: Date.now },
updatedOn: Date,
published: { type: Boolean, required: true, default: false },
content: { type: String, required: true, default: 'empty' },
category: { type: [CategoriesSchema], default: [CategoriesSchema] },
tags: [String],
images: [String]
});
mongoose.model('Post', PostSchmea);
mongoose.model('Categories',CategoriesSchema);
然后我在server.js文件中声明了模式和模型以运行代码并创建一条记录:
I then declared the Schemas and models in my server.js file to run the code and create a record:
//contents of my server.js file (or wherever you put your controllers)
var stack = require('./app/models/stackoverflow'),
post = mongoose.model('Post');
var v = new post({
//Define your values here
});
v.save(function(err,docs) {
if (err) {
console.log(err)
} else {
console.log(docs)
}
});
这成功创建了具有所有定义的默认值的记录.希望能有所帮助.
This successfully created the records with all of the defined default values. Hope that helped.
这篇关于猫鼬模式嵌入式文档默认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!