Tuesday, 2 May 2017

Why does mongoose still allow duplicate entries?

I have a mongoose schema set up like this :

var mongoose = require('mongoose');

var UrlSchema = new mongoose.Schema({
  url_name: {
    type: String,
    unique: true,
    required: true,
    index: true
  },
  url_address: {
    type: String,
    unique: true,
    required: true,
    index: true
  }
});

module.exports = mongoose.model('Url', UrlSchema);

From my understanding (Im a beginner), the unique, required and index options mean that no duplicate entries are allowed into the database. However, this is not the case here. I set up a simple API that takes in the request body and adds it to mongo database.

Here is the request body:

{
    "url_name":"testing",
    "url_address":"111.11.1.11"
}

However, I can add this same body multiple times without the duplicate error showing up. Any idea why?



via deeveeABC

No comments:

Post a Comment