Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose duplicates with the schema key unique

I want to make the key project unique across that collection but i cant getting this working, i found similar problem here.

task.js

function make(Schema, mongoose) {      var Tasks = new Schema({         project: { type: String, index: { unique: true, dropDups: true }},         description: String     });      mongoose.model('Task', Tasks); } module.exports.make = make; 

test.js

var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/rss');  var Schema = mongoose.Schema   , ObjectId = Schema.ObjectId;  require('./task.js').make(Schema, mongoose); var Task = mongoose.model('Task'); var newTask = new Task({     project: 'Starting new project'   , description: 'New project in node' }); newTask.save(function(err) {     if (err) console.log('Error on saving'); });  mongoose.disconnect(); 

When i run the app with node test.js, still creates duplicates.

MongoDB shell version: 2.0.2 connecting to: rss > db.tasks.find() > db.tasks.find() { "project" : "Starting new project", "description" : "New project in node", "_id" : ObjectId("4f21aaa3d48d4e1533000001") } { "project" : "Starting new project", "description" : "New project in node", "_id" : ObjectId("4f21aaa4d9a8921a33000001") } { "project" : "Starting new project", "description" : "New project in node", "_id" : ObjectId("4f21aaa57ebeea1f33000001") } 

// Edit still same problem, here's what i tried to do delete the db.tasks.drop() collection restart mongo sudo stop mongodb and start mongodb, ran the program again and still same problem, how does it allow duplicate data on index?

like image 918
Risto Novik Avatar asked Jan 26 '12 19:01

Risto Novik


People also ask

What is unique in Mongoose schema?

The unique option tells Mongoose that each document must have a unique value for a given path. For example, below is how you can tell Mongoose that a user's email must be unique. const mongoose = require('mongoose'); const userSchema = new mongoose.

How do I avoid duplicate errors in MongoDB?

If you ever faced this error all you need to do is to check your model carefully and find out that is there any unique key set true by you and if it is not necessary then simply remove the unique key from the model or otherwise set a unique value if it is necessary to be unique.

What does Mongoose unique validator do?

mongoose-unique-validator is a plugin which adds pre-save validation for unique fields within a Mongoose schema. This makes error handling much easier, since you will get a Mongoose validation error when you attempt to violate a unique constraint, rather than an E11000 error from MongoDB.

What is __ V 0 in Mongoose?

This is __v field that is only generated when a document(s) is inserted through mongoose. The __v field is called the version key. It describes the internal revision of a document. This __v field is used to track the revisions of a document. By default, its value is zero.


1 Answers

The Schema object you're passing may not work correctly because you are nesting 'unique' attribute into 'index' attribute, try something like this (it works as intended) :

User = mongoose.model('User', new Schema({     firstName:  {         type:String,         required: true,     },     lastName: {         type:String,         required: true,     },     email: {         type:String,         required: true,         unique: true     },     address: String,     phone: {         type:String,         required: true,     },     password:  {         type:String,         required: true,         set: Data.prototype.saltySha1 // some function called before saving the data     },     role: String },{strict: true})); 

Or more specifically for your example :

var Tasks = new Schema({     project: {          type: String,          unique: true,         index: true     },     description: String }); 

Note : I don't know what you're trying to do with the "dropDups" parameter, it doesn't seems to be in the mongoose documentation.

like image 117
Arnaud Rinquin Avatar answered Sep 28 '22 09:09

Arnaud Rinquin