【发布时间】:2011-12-28 01:13:42
【问题描述】:
在 mongoHQ 和 mongoose 上使用 node.js、mongodb。我正在为类别设置架构。我想使用文档 ObjectId 作为我的 categoryId。
var mongoose = require('mongoose');
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var Schema_Category = new Schema({
categoryId : ObjectId,
title : String,
sortIndex : String
});
然后我跑
var Category = mongoose.model('Schema_Category');
var category = new Category();
category.title = "Bicycles";
category.sortIndex = "3";
category.save(function(err) {
if (err) { throw err; }
console.log('saved');
mongoose.disconnect();
});
请注意,我没有为 categoryId 提供值。我假设猫鼬将使用模式来生成它,但文档具有通常的“_id”而不是“categoryId”。我做错了什么?
【问题讨论】: