1. The index is built on the number key named Nameindex and is a positive-order index ({number:-1} is a reverse index) such as: Db.list name. Ensureindex ({number:1},{name: "Nameindex"});
2. Specify the use of a positive-order index when querying hint () For example: Db.list name. Find ({age:{$lte: 56}},{name:1,_id:0}). Hint ({name:1});
3. At the time of query, which index do we use such as: Db.list name. Find ({age:{$lte:]). explain (); You can print out the index information used;
4. Indexing sometimes consumes performance then we can execute it in the background using the Db.list name. Ensureindex ({number:1},{background:true});
5. Create a unique index, meaning that the contents of each key of the index cannot have duplicates such as: Db.list name. ensureindex ({number:1},{unique:true,dropdups:true}); Dropdups:true control Delete (optional) If there are duplicates * * * But the meaning of the unique index is not large * * *;
6. Delete the specified index (name_-1 is the index name) such as: Db.runcommand ({dropindexes: "list name", Index: "Name_-1"}), complete the deletion of the index, or use the db.list name directly. Drop (); Also available;
If you want to delete all indexes such as: Db.runcommand ({dropindexes: "list name", Index: "*"});
The index on MongoDB