mongodb 猫鼬和独特的领域
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21971666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mongoose & unique field
提问by Julio
I have the following schema with mongoose:
我有以下 mongoose 模式:
var SimSchema = new Schema({
msisdn : { type : String , unique : true, required : true },
imsi : { type : String , unique : true, required : true },
status : { type : Boolean, default: true},
signal : { type : Number },
probe_name : { type: String , required : true }
});
I have unique
option for the msisdn
and imsi
.
我可以unique
选择msisdn
和imsi
。
In some cases this condition is well respected.
For the following mocha
test:
在某些情况下,这种情况得到了很好的尊重。对于以下mocha
测试:
"use strict";
var app = require('../../app');
var http = require('http');
var request = require('supertest');
var mongoose = require('mongoose');
var should = require('should');
describe('[ Sim controller ] ', function(){
before(function(done) {
app.set_env('test');
this.server = app.start(function() {
mongoose.connection.db.dropDatabase(function() {
done();
})
});
});
beforeEach(function(done){
done();
});
it('Sim with good parameters should be created in the database', function(done){
var newSim = {
msisdn: '1234',
imsi: '007',
probe_name: 'BOUCHON_1'
};
request(this.server)
.post('/sims')
.set('Content-Type', 'application/json')
.send(newSim)
.expect(200).end(function(err, res) {
if (err) return done(err);
res.body.should.have.property('imsi');
res.body.should.have.property('probe_name');
res.body.should.have.property('msisdn');
setTimeout(function() {
done();
}, 1000);
});
});
it('Sim imsi/msisdn is unique in the database', function(done){
var newSim = {
msisdn: '1234',
imsi: '007',
probe_name: 'BOUCHON_1'
};
request(this.server)
.post('/sims')
.set('Content-Type', 'application/json')
.send(newSim)
.expect(200).end(function(err, res) {
if (err) return done(err);
res.body.should.have.property('error').equal('Duplicate Item');
done();
});
});
after(function(done) {
app.stop(done);
});
});
It's working fine if I run it directly:
如果我直接运行它,它工作正常:
julio$ mocha test/controllers/ctrl_sim.js
But If I run it thanks to the recessive option it failed:
但是如果我由于隐性选项而运行它,它会失败:
1) [ Sim controller ] Sim imsi/msisdn is unique in the database:
Uncaught AssertionError: expected { __v: 0,
imsi: '007',
msisdn: '1234',
probe_name: 'BOUCHON_1',
_id: '530a2b7f52273aa90783baf0',
status: true } to have property 'error'
I read on stack that sometimes the unique
condition is not well respected as the index is not refreshed.
Do you think this is my case? In fact, I delete the database for each mocha test suite. Maybe mongo doesn't have the time to recreate all the indexes each time.
我在堆栈上读到,有时unique
由于索引没有刷新,条件没有得到很好的遵守。你认为这是我的情况吗?事实上,我删除了每个 mocha 测试套件的数据库。也许 mongo 没有时间每次都重新创建所有索引。
Any idea?
任何的想法?
回答by Hüseyin BABAL
Use dropDups
to ensure dropping duplicate records in your schemas like;
使用dropDups
,以确保您的架构像撤消重复的记录;
var SimSchema = new Schema({
msisdn : { type : String , unique : true, required : true, dropDups: true },
imsi : { type : String , unique : true, required : true, dropDups: true },
status : { type : Boolean, default: true},
signal : { type : Number },
probe_name : { type: String , required : true }
});
And before running your tests, restart mongodb
在运行测试之前,重新启动 mongodb