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');
            }
        });
    }
};
