我已经阅读了文档以及其他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 }
答案 0 :(得分:1)
解决方案:
var Bucket = mongoose.model('Bucket', bucketSchema);
Bucket.on('index', function() {
new Bucket({}).save();
new Bucket({}).save();
new Bucket({}).save();
new Bucket({}).save();
})
您还可以在安装阶段将create db分开,这是在使用之前获得索引的最佳方式。
答案 1 :(得分:0)
哦,男人,经过多次敲击后,我想出了这个。
问题是所有命令都是异步发生的!
这就是发生的事情:
你必须确保ensureIndex运行,并且在开始尝试写入索引之前实际上已经创建了索引。
利奥