|
Operations: Create IndexensureIndex() is the helper function for this. Its implementation creates an index by adding its info to the system.indexes collection. > use test > db.myCollection.ensureIndex(<keypattern>); > // same as: > db.system.indexes.insert({ name: "name", ns: "namespaceToIndex", key: <keypattern> }); Note: Once you've inserted the index, all subsequent document inserts for the given collection will be indexed, as will all pre-existing documents in the collection. If you have a large collection, this can take a significant amount of time and will block other operations.
You can query system.indexes to see all indexes for a collection foo in db test:
>db.system.indexes.find( { ns: "test.foo" } );
In some drivers, ensureIndex() remembers if it has recently been called, and foregoes the insert operation in that case. Even if this is not the case, ensureIndex() is a cheap operation, so it may be invoked often to ensure that an index exists. Dropping an IndexFrom the shell: db.mycollection.dropIndex(<name_or_pattern>)
db.mycollection.dropIndexes()
// example:
t.dropIndex( { name : 1 } );
From a driver (raw command object form; many drivers have helpers): { deleteIndexes: <collection_name>, index: <index_name> }
// "*" for <index_name> will drop all indexes except _id
ReIndexThe reIndex command will rebuild all indexes for a collection. db.myCollection.reIndex()
// same as:
db.runCommand( { reIndex : 'myCollection' } )
Usually this is unnecessary. You may wish to do this if the size of your collection has changed dramatically or the disk space used by indexes seems oddly large. Since 1.8 indexes will automatically be compacted as they are updated. reIndex is a blocking operation (indexes are rebuilt in the foreground) and will be slow for large collections. The repair database command recreates all indexes in the database. Index NamespaceEach index has a namespace of its own for the btree buckets. The namespace is: <collectionnamespace>.$<indexname> This is an internal namespace that cannot be queried directly. |

PLEASE POST QUESTIONS IN THE FORUMS: http://groups.google.com/group/mongodb-user. Post tips and clarifications here.
blog comments powered by Disqus