0

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?

deeveeABC
  • 960
  • 3
  • 12
  • 34
  • First thing to check - are the entries definitely being persisted to the database? – Joe Clay May 02 '17 at 15:31
  • possible duplicate of [How to stop insertion of Duplicate documents in a mongodb collection](http://stackoverflow.com/questions/24122981/how-to-stop-insertion-of-duplicate-documents-in-a-mongodb-collection) – Mateo Marconi May 02 '17 at 15:36
  • @JoeClay the data is added into the database correctly – deeveeABC May 02 '17 at 15:37

0 Answers0