猫鼬独特的领域 [英] Mongoose & unique field

查看:64
本文介绍了猫鼬独特的领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的猫鼬具有以下架构:

I have the following schema with 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 }
});

对于msisdnimsi,我具有unique选项.

在某些情况下,此条件受到尊重. 对于以下mocha测试:

In some cases this condition is well respected. For the following mocha test:

"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'

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

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.

有什么主意吗?

推荐答案

使用dropDups确保在诸如以下模式中删除重复记录;

Use dropDups to ensure dropping duplicate records in your schemas like;

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

And before running your tests, restart mongodb

这篇关于猫鼬独特的领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆