Database Reference
In-Depth Information
the index will be built in the background. however, the connection that initiates the request will be blocked, if
you issue the command from the mongoDB shell. a command issued on this connection won't return until the indexing
operation is complete. at first sight, this appears to contradict the idea that it is a background index. however, if you have
another mongoDB shell open at the same time, you will find that queries and updates on that collection run unhindered
while the index build is in progress. it is only the initiating connection that is blocked. this differs from the behavior you
see with a simple ensureIndex() command, which is not run in the background, so that operations on the second
mongoDB shell would also be blocked.
Note
KILLING the INDeXING prOCeSS
You can also kill the current indexing process if you think it has hung or is otherwise taking too long. You can do
this by invoking the killOp() function:
> db.killOp(<operation id>)
to run a killOp you need to know the operation iD of the operation. You can get a list of all of the currently running
operations on your mongoDB instance by running the db.currentOp() command.
note that when you invoke the killOp() command, the partial index will also be removed again. this prevents
broken or irrelevant data from building up in the database.
Creating an Index with a Unique Key {unique:true}
When you specify the unique option, MongoDB creates an index in which all the keys must be different. This means
that MongoDB will return an error if you try to insert a document in which the index key matches the key of an
existing document. This is useful for a field where you want to ensure that no two people can have the same identity
(that is, the same userid ).
However, if you want to add a unique index to an existing collection that is already populated with data, you must
make sure that you have deduped the key(s). In this case, your attempt to create the index will fail if any two keys are
not unique.
The unique option works for simple and compound indexes, but not for multi-key value indexes, where they
wouldn't make much sense.
If a document is inserted with a field missing that is specified as a unique key, then MongoDB will automatically
insert the field, but will set its value to null . This means that you can only insert one document with a missing key
field into such a collection; any additional null values would mean the key is not unique, as required.
Dropping Duplicates Automatically with {dropdups:true}
If you want to create a unique index for a field where you know that duplicate keys exist, you can specify the dropdups
option. This option instructs MongoDB to remove documents that would cause the index creation to fail. In this
case, MongoDB will retain the first document it finds in its natural ordering of the collection, but then drop any other
documents that would result in an index-constraint violation.
 
 
Search WWH ::




Custom Search