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

我有 msisdnimsiunique选项。

在某些情况下,此条件得到很好的遵守。 对于以下 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);
  });
});

直接运行就可以了:

julio$ mocha test/controllers/ctrl_sim.js 

但是如果我通过隐性选项运行它,它会失败:

  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'

我在堆栈上读到,有时 unique 条件没有得到很好的尊重,因为索引没有刷新。 你认为这是我的情况吗?事实上,我删除了每个 mocha 测试套件的数据库。也许 mongo 没有时间每次都重新创建所有索引。

有什么想法吗?

最佳答案

使用 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 }
});

在运行测试之前,重启 mongodb

关于mongodb - Mongoose 和独特的领域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21971666/

相关文章:

javascript - 如何在 Mongoose 模型中定义方法?

windows - 如何在 Windows 中连接到 MongoDB?

mongodb - 是否可以展平 MongoDB 结果查询?

c# - MongoDB 和 C# : Case insensitive search

node.js - mongo 可以 upsert 数组数据吗?

ubuntu - 为什么我不能启动mongodb

mongodb - mongod.service 启动失败 : Unit mongod. 找不到服务

mongodb - MongoDB 中的 replaceOne() 和 updateOne() 有什

shell - MongoDB 正在运行但无法使用 shell 连接

java - 自动配置数据源失败 : 'spring.datasource.url' is not