3

I am getting one error related to mongodb.Actually i have one collection whose datas are given below.

"_id" : ObjectId("55bd9b7adfa5d64c057de74f"),
 "username" : "admin",
 "emailid" : "*****************************",
 "password" : "adminuser",
 "image" : "images/Image0895.jpg",
 "firstname" : "subhrajyoti",
 "lastname" : "pradhan"

Here i want to keep the username document as unique.For that i wrote the commanddb.collectionname.createIndex( {"username":"admin"},{unique:true}).But it gave me the following error.

Error:

        "ok" : 0,
        "errmsg" : "bad index key pattern { username: \"admin\" }: Unknown index
 plugin 'admin'",
        "code" : 67

Please help me to resolve this error keep my one document as unique for this collection.

1 Answer 1

5

For a unique index on username field you'd use this command:

db.collectionname.createIndex( {"username": 1},{unique:true}) 

The value of the username key must be 1 (indexed ascending) or -1 (indexed descending)

See MongoDB docs on db.collection.createIndex()

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.