I want the project field of my document is unique across the whole collection but I am unable to implement that:
Refer to my code for 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;
Here is my code for 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();
I have run the app as:
node test.js
But it still generates replicas of the project field.
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") }
I also tried to delete the db.tasks.drop() collection and then restart mongo sudo. But it continues to generate duplicates!
Kindly help me... Thanks in advance.