【问题标题】:MongoDB, using Mongoose duplicate key error index?MongoDB,使用 Mongoose 重复键错误索引?
【发布时间】:2014-04-16 21:31:19
【问题描述】:

我有一个 MEN(MongoDB、Express、Node)应用程序,当我插入第一个项目时一切正常,但当我想添加第二个项目时出现此错误:

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

如果我然后删除第一行,然后再次插入一个项目,它可以工作,但是当我想添加第二个项目时,我一直收到错误。

我真的在这个问题上拉了我的头发,似乎找不到问题:(

希望有人可以帮助我:)

顺便说一句,我如何从命令行访问我的表?我尝试了db.movies.find()movies.find() 和其他选项,但没有任何反应,当我输入show collestions 时,我只得到system.indexes。 以及如何从命令行删除表并尝试创建一个新表?

这是我的模型:

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

这是我的架构:

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;

我也试过没有索引,movieTitle: { type: String, required: true }

这是我的插入/更新功能:

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

【问题讨论】:

  • 哇!请告诉我们你做了什么导致错误。太吵了。请编辑。
  • 不确定要我编辑什么?
  • @user1906437 不要编辑任何东西——这足以帮助我解决问题,尽管我无法解决你的问题我确实从你的帖子中学到了一些东西——你最终做了什么?

标签: node.js mongodb express mongoose


【解决方案1】:

Mongoose 不会重新创建索引

因此,如果您创建了一个唯一性等于 true 的索引,则在删除索引或删除 db 之前无法更改它。

因此,只需删除您的数据库或标题索引并使用 unique=false 重新创建它

【讨论】:

    【解决方案2】:

    movieTitle 的索引为 unique:true。该索引在应用程序第一次启动时创建。其余时间,猫鼬会检查索引是否存在。如果没有,它将创建它。

    当您删除唯一声明时,猫鼬不会像您期望的那样重新创建索引。也许在功能版本中它会。

    您必须手动删除索引 (see mongo docs) 或整个数据库。

    重新启动您的应用后,将创建新索引。

    希望对你有帮助

    【讨论】:

    • 感谢您的回复,但我不确定该怎么做,而且似乎无法通过命令行、删除索引或整个数据库访问我的集合。
    • 你有本地的 mongodb 安装吗?
    • 从命令行你可以尝试“mongo ”然后db.dropDatabase()
    猜你喜欢
    • 2020-09-29
    • 2018-01-19
    • 2016-10-31
    • 2018-10-12
    相关资源
    最近更新 更多