Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 250
Julio Avatar asked Feb 23 '14 17:02

Julio


People also ask

What is Mongoose used for?

Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node. js. It manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB. MongoDB is a schema-less NoSQL document database.

How big is a Mongoose?

Mongoose are a weasel-like animal totaling about 26″ in length with a long, brownish body, short legs and a tail as long as its body. They have small rounded ears and a pointed nose.

Is a Mongoose a bird?

A mongoose is a small terrestrial carnivorous mammal belonging to the family Herpestidae.

What is Mongoose in node JS?

Mongoose is a Node.js-based Object Data Modeling (ODM) library for MongoDB. It is akin to an Object Relational Mapper (ORM) such as. SQLAlchemy. for traditional SQL databases. The problem that Mongoose aims to solve is allowing developers to enforce a specific schema at the application layer.


1 Answers

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

like image 103
Hüseyin BABAL Avatar answered Sep 19 '22 18:09

Hüseyin BABAL