开发者

MongoDB : ensureIndex not working on both single key and composite keys

开发者 https://www.devze.com 2023-01-25 19:46 出处:网络
From hours I\'m trying to add bulk data in my locally setup MongoDB collection, all is set except unique index for a key or keys :(

From hours I'm trying to add bulk data in my locally setup MongoDB collection, all is set except unique index for a key or keys :(

I've used this:

ensureIndex({appid:1, userid:1}, {unique:true, dropDups:true, background:true})

then this (coz I guess for whatever reason, multiple keys index is not working)

ensureIndex({userid:1}, {开发者_Python百科unique:true, dropDups:true, background:true})

after using above lines I tried to insert millions of rows from a php script (via both insert and batchInsert) and everytime duplicate userids are there :'( please please guide me on this :(


I have noticed that ensureIndex will not overwrite an existing index. Instead, I believe you must first delete the existing index and THEN run ensureIndex.


It is probably a good idea to look and make sure the index was created. By default write operations (like creating indexes) are fire-and-forget and don't wait for error/success. This could be a bug in the driver (php client lib).

You can run

db.coll.getIndexes()

to list the indexes for the collection.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号