[DOCS-694] problem about creating unqiue index with "dropDups" option Created: 30/Oct/12  Updated: 03/Nov/17  Resolved: 06/Nov/12

Status: Closed
Project: Documentation
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: auto Assignee: Sam Kleinman (Inactive)
Resolution: Done Votes: 0
Labels: collector-298ba4e7
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Mongodb: 2.2.0
Linux: 2.6.32-279.9.1.el6.x86_64 (CentOS)

Location: http://cn.docs.mongodb.org/manual/
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4
Referrer: http://cn.docs.mongodb.org/manual/reference/method/
Screen Resolution: 1440 x 900


Participants:
Days since reply: 11 years, 15 weeks, 1 day ago

 Description   

db.people.insert([

{ "_id" : ObjectId("508f639c086512dc1f8694bd"), "username" : "smith", "age" : 48, "user_id" : 0 }

,

{ "_id" : ObjectId("508f63a1086512dc1f8694be"), "username" : "smith", "age" : 30, "user_id" : 1 }

,

{ "_id" : ObjectId("508f63ae086512dc1f8694bf"), "username" : "john", "age" : 36, "user_id" : 2 }

,

{ "_id" : ObjectId("508f63b9086512dc1f8694c0"), "username" : "john", "age" : 18, "user_id" : 3 }

,

{ "_id" : ObjectId("508f63ca086512dc1f8694c1"), "username" : "joe", "age" : 36, "user_id" : 4 }

,

{ "_id" : ObjectId("508f63d5086512dc1f8694c2"), "username" : "john", "age" : 7, "user_id" : 5 }

,

{ "_id" : ObjectId("508f63e0086512dc1f8694c3"), "username" : "simon", "age" : 3, "user_id" : 6 }

,

{ "_id" : ObjectId("508f63ee086512dc1f8694c4"), "username" : "joe", "age" : 27, "user_id" : 7 }

,

{ "_id" : ObjectId("508f63f9086512dc1f8694c5"), "username" : "jacob", "age" : 17, "user_id" : 8 }

,

{ "_id" : ObjectId("508f6404086512dc1f8694c6"), "username" : "sally", "age" : 52, "user_id" : 9 }

,

{ "_id" : ObjectId("508f640d086512dc1f8694c7"), "username" : "simon", "age" : 59, "user_id" : 10 }

])

db.people.ensureIndex(

{"age": 1}

,

{"dropDups": true}

) # step 1
db.people.ensureIndex(

{"age": 1}

,

{"unique": true, "dropDups": true}

) # step 2

The step 2 doesn't take effect after I run step 1...

Reporter: Ryououki
E-mail: ytao_s@hotmail.com



 Comments   
Comment by auto [ 06/Nov/12 ]

Author:

{u'date': u'2012-11-06T18:24:13Z', u'email': u'samk@10gen.com', u'name': u'Sam Kleinman'}

Message: DOCS-694 adding note about ensure index behavior
Branch: master
https://github.com/mongodb/docs/commit/07d11ea8a577aced24a5e3e991495a331f93ff76

Comment by Sam Kleinman (Inactive) [ 30/Oct/12 ]

That's correct.

The ensureIndex will not rebuild an index if that index already exists with that field. In this situation, you would need to drop the index and recreate it with the desired options. On a related note, as a general rule, it's a good idea to not call the ensureIndex option as part of application startup in production systems: at best it's a no-op, and at worst a typo or change in configuration can render a production system inaccessible while it builds an unnecessary index.

I'll make a change to the docs soon that will underscore both of these aspects of the ensureIndex command.

Generated at Thu Feb 08 07:39:18 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.