0

I don't understand why I got this error below after adding the first entry:

E11000 duplicate key error index: mydb.datasets.$id_1  dup key: { : null }

I don't have any null value in my first entry:

{
    "index" : "9IPZMW7IL",
    "name" : "Tweets",
    "owner_name" : "xxx",
    "read_key" : "fb6f9125f4ca15c33fea89416c3351d1",
    "write_key" : "d8a6c7e5fc73b5a91aa7a533565ed1f1",
    "data" : {
        "var1" : {
            "name" : "particles"
        }
    },
    "_id" : ObjectId("57729dc20cb70952424cdbb4"),
    "created_at" : ISODate("2016-06-28T15:54:42.576Z"),
    "entries_number" : 0,
    "public" : true,
    "__v" : 0
}

Below is my code:

// CRUD API:
// POST/ Create new dataset request
router.post("/", helper.authenticate, function(req, res) {
    // Used to set the dataset owner
    var sessionUser = req.session.user.name;
    // Get values from the post request
    var name = req.body.name;
    var isPublic = req.body.public != undefined ? true:false;
    // Delete the values from the request body so that we only keep information about the variables
    delete req.body.name;
    delete req.body.public;

    // This is so that we can loop through the object in reverse order
    // We do that so that the fields are saved in the right order on the db
    // (this way it will appear in the right order on the 'edit' view)
    var propertiesList = [];
    for (var property in req.body) {
        if (req.body.hasOwnProperty(property)) {
            propertiesList.push(property);
        }
    }
    propertiesList.reverse();

    var variablesFields = {};
    for (var i in propertiesList) {
        console.log(propertiesList[i])
        variablesFields[propertiesList[i]] = {name:req.body[propertiesList[i]],
                                    values: Array};
    }

    // Create dataset
    Dataset.create({
        index: helper.uniqueIndex(),
        name: name,
        owner_name: sessionUser,
        read_key: hat(),
        write_key: hat(),
        public: isPublic,
        data: variablesFields
    }, function(err, dataset) {
        if (err) {
            console.log("Error creating the dataset: " + err);
            req.session.error = "A problem occured when creating the dataset. Please try again.";
        } else {
            console.log("New dataset created with id: " + dataset._id);
            req.session.success = "Dataset " + name + " created successfully.";
        }
        res.redirect("/index");
    });
});

Error:

Error creating the dataset: WriteError({"code":11000,"index":0,"errmsg":"E11000 duplicate key error index: mydb.datasets.$id_1 dup key: { : null }","op":{"index":"2IPZMWHGI","name":"PM 2","owner_name":"xxx","read_key":"fc31c152aa86070252c70c0304e4ca5c","write_key":"238110753c8762ce4a547fa02100a299","data":{"var1":{"name":"particles"}},"_id":"57729dcf0cb70952424cdbb5","created_at":"2016-06-28T15:54:55.459Z","entries_number":0,"public":true,"__v":0}})

Model:

var datasetSchema = new mongoose.Schema({
    index: {type: String, required: true, index: {unique: true}},
    name: {type: String, required: true},
    owner_name: {type: String, required: true},
    read_key: {type: String},
    write_key: {type: String},
    public: {type: Boolean, default: false},
    data: {type: Object},
    entries_number: {type: Number, default: 0},
    created_at: {type: Date, default: Date.now},
    last_entry_at: {type: Date}
});

Any idea why and how I can fix this?

4
  • You probably once had a property id in your schema that had unique : true. Now that you renamed it (to index?), the old unique index is still there. See this question. Commented Jun 28, 2016 at 16:33
  • It works fine after dropping the entire db and start with a fresh db. mongo is odd! Commented Jun 29, 2016 at 3:52
  • Did you try and remove the offending index first? Commented Jun 29, 2016 at 6:51
  • @robertklep I did but no luck so I had to drop the db and start anew. Now I came across a even weirder problem with mongo - stackoverflow.com/questions/38090771/… Commented Jun 29, 2016 at 14:00

1 Answer 1

1

I solved it by removing a "id" key that I initially declared as

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

I removed this line and deleted the initial collection and that solved the issue.

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

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.