4

I have a MEN (MongoDB, Express, Node) application, and when i'm inserting the first item everything is fine, but when i want to add a second one I get this error:

E11000 duplicate key error index: local.movies.$title_1 dup key: { : null }

If I then delete the first row, and then insert one item again it works, but again when I want to add a second item I keep getting the error.

I am really pulling my hair on this one, and can't seem to find the problem :(

Hopefully someone can help me :)

Btw how can I access my table from command line? I tried db.movies.find(), movies.find() and other options, but nothing happens, and when i type show collestions I only get system.indexes. And how can i delete the table from command line and try to make a new one?

This is my model:

var mongoose = require('mongoose');
var MovieSchema = require('../schemas/movie');
var Movies = mongoose.model('Movie', MovieSchema);
module.exports = Movies;

This is my schema:

var mongoose = require('mongoose');
var MovieSchema = new mongoose.Schema(
        {
            movieTitle: { type: String, required: true, index: { unique: true } },
            movieYear: { type: String, required: true },
            movieDirector: { type: String, required: true },
            movieImdbLink: { type: String, required: true }
        }
    );
    module.exports = MovieSchema;

I've also tried without index, movieTitle: { type: String, required: true }

This is my insert/update function:

// insert/update movie
exports.save = function(req, res){
    var _id = (req.body._id !== 'undefined') ? req.body._id : null;
    var movieTitle = req.body.movieTitle;
    var movieYear = req.body.movieYear;
    var movieDirector = req.body.movieDirector;
    var movieImdbLink = req.body.movieImdbLink;
    var movie = null;
    if(_id){
        var query = {_id: _id};
        Movies.update(
            query,
            {
                $set:{
                    movieTitle: movieTitle,
                    movieYear: movieYear,
                    movieDirector: movieDirector,
                    movieImdbLink: movieImdbLink
                }
            },
            {safe:true},
            function(err, result, raw){
                if(err){
                    console.log(err);
                    res.send(500);
                }
                else{
                    res.send(200);
                }
            }
        );
    }
    else{
        movie = new Movie(
            {
                movieTitle: movieTitle,
                movieYear: movieYear,
                movieDirector: movieDirector,
                movieImdbLink: movieImdbLink
            }
        );
        movie.save(function(err){
            if(err){
                res.redirect('/movies/?msg=' + err.message);
            }
            else{
                res.redirect('/movies');
            }
        });
    }
};
3
  • Whoa there! Please just show us what you did that resulted in the error. Too much noise. Please edit. Commented Mar 12, 2014 at 15:58
  • Not sure what you want me to edit? Commented Mar 12, 2014 at 20:48
  • @user1906437 don't edit anything - this exactly enough noise to help me out although I can't solve your problem I did learn something from your post - what did you end up doing? Commented Jul 10, 2014 at 12:37

2 Answers 2

3

movieTitle has an index with unique:true. This index is created when the app is started for the first time. The rest of the times, mongoose checks to see if the index exists. If it doesn't it will create it.

When you remove the unique declaration, mongoose doesn't recreate the index as you would excpect. Maybe in feature versions it will.

You have to manually drop the index (see mongo docs), or the whole db.

After restarting your app the new index will be created.

I hope that helps

Sign up to request clarification or add additional context in comments.

4 Comments

Thx for your reply, but I'm not sure what to do, and can't seem to get access to my collections by commandline, t drop indexes or the whole db.
Do you have a local mongodb installation?
From the command line you can try "mongo <thenameofmydb>" and then db.dropDatabase()
In MongoDB Compass on Windows, you can go to the collection, and then remove the field removed from unique declaration
0

Mongoose doesn't recreate index

So, if you had created a index with unique equal to true it cant be changed until either index is dropped or db is dropped.

So simply drop either your database or tittle index and recreate it with unique=false

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.