Mongoose Schema嵌入式文档默认值

时间:2015-06-27 19:04:19

标签: node.js mongodb mongoose

我正在尝试嵌入一个文档,所以我猜一个子文档,进入一个Mongoose模式。我一直在遵循Mongoose documentation建议的这种模式。我的模式看起来像这样。

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:无法读取属性' $ __'未定义":

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]
});

我还尝试了以下操作并得到了错误"语法错误:意外的令牌":

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]
});

非常感谢任何建议或文件。

谢谢,

1 个答案:

答案 0 :(得分:0)

我认为问题可能在于您的依赖声明。我使用你的代码来声明mongoose模式,并且它工作正常,但你没有包含你的依赖项或你的创建方法,所以让我告诉你我是如何创建记录的,我希望这有帮助。您还可以为ObjectId的依赖声明引用此Stack Overflow答案: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文件中声明了Schemas和models来运行代码并创建一条记录:

//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)
}
});

这成功创建了包含所有已定义默认值的记录。希望有所帮助。