E11000重复密钥错误集合:

时间:2017-04-20 08:36:00

标签: javascript node.js

下面这段代码很好用。但是,当我从我的cmd(节点服务器)再次运行它时,我收到了菜名的重复密钥消息。我有两个文件。 dish.js我在哪里定义我的模式,并可用于我的第二个名为server.js的文件。

    // grab the things we need
    var mongoose = require('mongoose');
    var Schema = mongoose.Schema;

    var commentSchema = new Schema({
        rating:  {
            type: Number,
            min: 1,
            max: 5,
            required: true
        },
        comment:  {
            type: String,
            required: true
        },
        author:  {
            type: String,
            required: true
        }
    }, {
        timestamps: true
    });
    // create a schema
    var dishSchema = new Schema({
        name: {
            type: String,
            required: true,
            unique: true
        },
        description: {
            type: String,
            required: true
        },
        comments:[commentSchema]
    }, 
    {
        timestamps: true
    });

    // the schema is useless so far
    // we need to create a model using it
    var Dishes = mongoose.model('Dish', dishSchema);

    // make this available to our Node applications
    module.exports = Dishes;

和我的 server.js 文件。

            var mongoose = require('mongoose'),
            assert = require('assert');

        var Dishes = require('./models/dishes-3');

        // Connection URL
        var url = 'mongodb://localhost:27017/conFusion';mongoose.connect(url);
        var db = mongoose.connection;
        db.on('error', console.error.bind(console, 'connection error:'));
        db.once('open', function () {
            // we're connected!
            console.log("Connected correctly to server");

            // create a new dish
            Dishes.create({
                name: 'Uthapizza',
                description: 'Test',
                comments: [
                    {
                        rating: 3,
                        comment: 'This is insane',
                        author: 'Matt Daemon'
                    }
                ]
            }, function (err, dish) {
                if (err) throw err;
                console.log('Dish created!');
                console.log(dish);

                var id = dish._id;

                // get all the dishes
                setTimeout(function () {
                    Dishes.findByIdAndUpdate(id, {
                            $set: {
                                description: 'Updated Test'
                            }
                        }, {
                            new: true
                        })
                        .exec(function (err, dish) {
                            if (err) throw err;
                            console.log('Updated Dish!');
                            console.log(dish);

                            dish.comments.push({
                                rating: 5,
                                comment: 'I\'m getting a sinking feeling!',
                                author: 'Leonardo di Carpaccio'
                            });

                            dish.save(function (err, dish) {
                                console.log('Updated Comments!');
                                console.log(dish);

                                db.collection('dishes').drop(function () {
                                    db.close();
                                });
                            });
                        });
                }, 3000);
            });
        });

如果你在server.js文件中密切注意,我已经从dishes.js文件中删除了 unique:true 属性,但我仍然遇到同样的问题。

    name: {
        type: String,
        required: true,
        unique: true
    },

1 个答案:

答案 0 :(得分:0)

下面给出了您的架构

name: {
        type: String,
        required: true,
        unique: true
    }

唯一正在运作

下面给出了您的架构

name: {
        type: String,
        required: true
    }

独特的不工作

更改架构定义后,删除所有集合并尝试插入。

相关问题