Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

mongodb - Mongoose & unique field

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

I have uniqueoption for the msisdn and imsi.

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'

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.

Any idea?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

And before running your tests, restart mongodb


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...