作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我已经阅读了文档以及其他 Stack Overflow 帖子,但我无法弄清楚我在这里做错了什么......这应该是一个简单的验证来检查唯一的字符串并根据对于文档和其他 Stack Overflow 文章,这应该会引发错误并且不会保存重复记录。在这种情况下,它似乎根本无法验证...
我曾尝试删除整个集合并再次运行该应用程序,结果却发现了相同的行为。
我正在按照 this post 中描述的推荐验证方式进行操作.
在此先感谢您对我的帮助。 NodeJS 新手。
bucket.js:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bucketSchema = new Schema({
name: {
type: String,
index: true,
required: true,
unique: true,
trim: true
},
created_at: {
type: Date,
default: Date.now
}
});
var Bucket = mongoose.model('Bucket', bucketSchema);
module.exports = Bucket;
app.js
var mongoose = require('mongoose');
// Require data models
var Bucket = require('./models/bucket.js');
// Connect to database
mongoose.connect("mongodb://localhost/tritonia");
mongoose.set('debug' ,true);
// Test
var bucket = new Bucket({ name: "foo" });
bucket.save(function(err) {
if (err) {
console.log(err);
} else {
console.log("Saved");
}
});
var bucket2 = new Bucket({ name: "foo" });
bucket2.save(function(err) {
if (err) {
console.log(err);
} else {
console.log("Test failed: It should not save a second bucket named 'foo'");
}
})
输出:
Express server listening on port 3000
Mongoose: buckets.ensureIndex({ name: 1 }) { safe: undefined, background: true, unique: true }
Mongoose: buckets.insert({ __v: 0, created_at: new Date("Mon, 16 Dec 2013 02:27:39 GMT"), _id: ObjectId("52ae651baabf290436000001"), name: 'foo' }) {}
Mongoose: buckets.insert({ __v: 0, created_at: new Date("Mon, 16 Dec 2013 02:27:39 GMT"), _id: ObjectId("52ae651baabf290436000002"), name: 'foo' }) {}
Saved
Test failed: It should not save a second bucket named 'foo'
数据库控制台:
> db.buckets.find()
{ "name" : "foo", "_id" : ObjectId("52ae6427a58628cd34000001"), "created_at" : ISODate("2013-12-16T02:23:35.052Z"), "__v" : 0 }
{ "name" : "foo", "_id" : ObjectId("52ae6427a58628cd34000002"), "created_at" : ISODate("2013-12-16T02:23:35.056Z"), "__v" : 0 }
{ "name" : "foo", "_id" : ObjectId("52ae6447d0ac5a0e35000001"), "created_at" : ISODate("2013-12-16T02:24:07.413Z"), "__v" : 0 }
{ "name" : "foo", "_id" : ObjectId("52ae6447d0ac5a0e35000002"), "created_at" : ISODate("2013-12-16T02:24:07.416Z"), "__v" : 0 }
{ "name" : "foo", "_id" : ObjectId("52ae64f02876b7d535000001"), "created_at" : ISODate("2013-12-16T02:26:56.642Z"), "__v" : 0 }
{ "name" : "foo", "_id" : ObjectId("52ae64f02876b7d535000002"), "created_at" : ISODate("2013-12-16T02:26:56.645Z"), "__v" : 0 }
{ "name" : "foo", "_id" : ObjectId("52ae650caeda67f435000001"), "created_at" : ISODate("2013-12-16T02:27:24.646Z"), "__v" : 0 }
{ "name" : "foo", "_id" : ObjectId("52ae650caeda67f435000002"), "created_at" : ISODate("2013-12-16T02:27:24.689Z"), "__v" : 0 }
{ "name" : "foo", "_id" : ObjectId("52ae651baabf290436000001"), "created_at" : ISODate("2013-12-16T02:27:39.136Z"), "__v" : 0 }
{ "name" : "foo", "_id" : ObjectId("52ae651baabf290436000002"), "created_at" : ISODate("2013-12-16T02:27:39.140Z"), "__v" : 0 }
最佳答案
解决方法:
var Bucket = mongoose.model('Bucket', bucketSchema);
Bucket.on('index', function() {
new Bucket({}).save();
new Bucket({}).save();
new Bucket({}).save();
new Bucket({}).save();
})
您也可以在安装阶段单独创建数据库,这是在使用前先有索引的最佳方式。
关于node.js - 简单的 Mongoose 唯一验证我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20602615/
我是一名优秀的程序员,十分优秀!