简体   繁体   中英

collection.insert issue in Meteor?

I am tries to insert information to a collection in this time gets error as shown below :

Error :

[01:17:33.480] "Error :MongoError: E11000 duplicate key error index: meteor.Dcare_user.$UserId_1 dup key: { : null }"

Collection.js :

Dcare_User = new Meteor.Collection("dcare_user");

Js Code :

var doc = Dcare_User.insert({
         userid : UserId,
         firstname : fname,
         lastname : lname,
         dob : dob,
         address : address,
         phoneno : phno,
         city : city,
         state : state,
         country : country,
         zipcode : zipcode,
         ssn : ssn,
         roletype : permissions,
         isactive : isactive,
}, function( error, result) {  
     if ( error ) console.log ( "Error :"+error.reason ); //info about what went wrong 
     if ( result ) { 
       console.log ( "result="+result );//the _id of new object if successful                        
     }                     
});

I am new to meteor.So i didn't get any idea about this.So please help me.

Problem

This error means:

  • There is unique index on Dcare_user.UserId .
  • There exists a document in the Dcare_user collection with UserId equal to null .
  • You are trying to insert a new document with UserId equal to null .

It looks like an unique index was created on a field you are not using.

Solution

Either you need to drop the database and start fresh, or keep the database and remove the index. The following solutions assume this is a development database created for you by the meteor environment:


Drop the database

If you wish to drop the database and start over, you can type the following into your terminal from your app's root directory (while meteor is not running):

$ meteor reset

Remove the index

If you wish to remove the index and keep your data you can connect to the mongo shell by typing the following into your terminal from your app's root directory (while meteor is running):

$ meteor mongo

Once you are in the shell, you can remove the index with this command:

> db.Dcare_user.dropIndex({UserId:1});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM