0

I am trying to create a blog. I have a user schema, a blog schema, and a comment schema.

When I register a user and create a blog, it works (and saves fine to the database). When I create another user and that user tries to write a blog, I get returned a large error message:

BulkWriteError: E11000 duplicate key error collection: blog.blogs index: username_1 dup key: { : null }

The problem is - there is no key in any of my schema's called username_1. here are my schema's:

var UserSchema = new mongoose.Schema({
    firstname: String,
    lastname: String,
    username: {
        type: String,
        unique: true
    },
    email: String,
    createdDate: {
        type: Date,
        default: Date.now()
    },
    blogs: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: "Blog"
        }
    ]
});

Blog schema

var BlogSchema = new mongoose.Schema({
    title: String,
    text: String,
    date: {
        type: Date,
        default: Date.now()
    },
    comments: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Comment'
        }
    ],
    author: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "User"
    }
});

The post route is this in case you want to know:

// create a new blog object
            var newBlog = new Blog(
                {
                    title : req.body.title,
                    text : req.body.text,
                    author: foundUser._id
                }
            );

            // create the new blog
            Blog.create(newBlog, function(err, createdBlog) {
                if(err) {
                    console.log(err);
                } else {
                    // push the new blog into the blogs array
                    foundUser.blogs.push(createdBlog);

                    // save to db
                    foundUser.save();
                }
            });
Keren Caelen
  • 1,466
  • 3
  • 17
  • 38
J.G.Sable
  • 1,258
  • 4
  • 26
  • 62
  • Putting "unique" on a property means that you need to have a value in it **always**. The error is because you already have some content in the collection which has no "username". Just like you are trying to do again. if `"username"` is not actually "required" then you should remove the index. MongoDB appends the `1` to the name of the field in the index as the index name by default ( unless you name it otherwise ). The `1` indicates "ascending" order. – Neil Lunn May 03 '18 at 04:30
  • Thank you, but I don't understand what I need to change. I know you're telling me the answer... – J.G.Sable May 03 '18 at 05:06
  • Go into the shell, select your database and type `db.blogs.dropIndexes()`. At some earlier time you had "username" within your "blog" schema and that created the index. Mongoose does not remove indexes from the database when you remove things from your schema. Drop the indexes and let mongoose create the correct ones ( if any ) on start up again. Would have been mentioned in the answers, so you should read them. You're not the first to ever make the same mistake. – Neil Lunn May 03 '18 at 05:11
  • wow. that worked. thank you. So dropping a database (as I've been doing when I change my model schema) doesn't reset the indexes? – J.G.Sable May 03 '18 at 15:27
  • @NeilLunn it's happening again...this time dropping indexes, deleting the database isn't solving the issue. – J.G.Sable May 03 '18 at 23:09

1 Answers1

0

Actually, I think there is a unique key in your schema.

username: { type: String, unique: true },

Maybe you can try to go though the whole thing with different usernames?

tung yu
  • 94
  • 4