??
Introduction to Indexes
MongoDB, like traditional database indexes, also uses the B-tree index, and the vast majority of optimized mysql/oracle/sqllite indexing techniques apply to MONGODB as well.
To create an index using the Ensureindex method
Create a normal index
> Db.users1.find ()
{"_id": 1, "username": "Smith", "Age": $, "user_id": 0}
{"_id": 2, "username": "Smith", "age": +, "user_id": 1}
{"_id": 3, "username": "John", "Age": $, "user_id": 2}
{"_id": 4, "username": "John", "Age": +, "user_id": 3}
{"_id": 5, "username": "Joe", "Age": $, "user_id": 4}
{"_id": 6, "username": "John", "Age": 7, "user_id": 5}
{"_id": 7, "username": "Simon", "Age": 3, "user_id": 6}
{"_id": 8, "username": "Joe", "age": +, "user_id": 7}
{"_id": 9, "username": "Jacob", "age": +, "user_id": 8}
{"_id": Ten, "username": "Sally", "Age": "user_id": 9}
{"_id": One, "username": "Simon", "Age": A, "user_id": 10}
--Create a positive-order index on the username field
> Db.users1.ensureIndex ({"username": 1})
--Create a valid index on the following query
> Db.people.find ({"Date": Date1}). Sort ({"Date": 1, "username": 1})
> Db.people.ensureIndex ({"Date": 1, "username": 1})
Create index inline Document index
--embedded documents are as follows
> Db.blog.posts.findOne ({"Comments.name": "Licz"})
{
"_id": ObjectId ("4b2d75476cc613d5ee930164"),
"Comments": [
{
"Content": "nice post.",
"Date": Isodate ("2016-02-17t08:01:43.813z"),
"Email": "[email protected]",
"Name": "Joe",
"Visits": 1
},
{
"Content": "good post.",
"Date": Isodate ("2016-02-17t08:00:45.746z"),
"Email": "[email protected]",
"Name": "Licz"
}
],
"Content": "...",
"title": "A blog post"
}
> Db.bolg.posts.ensureIndex ({"Comments.date": 1})
Create a unique index
> Db.people.ensureIndex ({"username": 1}, {"Unique": true})
--Eliminate duplication
When you create a unique index for an existing collection, some may have been duplicated. At this point, the creation of the index will fail, sometimes it may be necessary to delete the duplicate documents, the Dropdups option can keep the first document found, and delete the duplicate documents.
> Db.people.ensureIndex ({"username": 1}, {"Unique": true, "dropdups": true})
Using explain and hint
Explain will return the query using the index, time consuming and scan the document statistics.
> Db.users1.find ({"username": "Joe"})
{"_id": 5, "username": "Joe", "Age": $, "user_id": 4}
{"_id": 8, "username": "Joe", "age": +, "user_id": 7}
> Db.users1.find ({"username": "Joe"}). Explain ()
{
"Cursor": "Btreecursor username_1",
"Ismultikey": false,
"N": 2,
"Nscannedobjects": 2,
"Nscanned": 2,
"Nscannedobjectsallplans": 2,
"Nscannedallplans": 2,
"Scanandorder": false,
"IndexOnly": false,
"Nyields": 0,
"Nchunkskips": 0,
"Millis": 3,
"Indexbounds": {
"Username": [
[
"Joe",
"Joe"
]
]
},
"Server": "racdb:27017"
}
If you find that MongoDB uses an unintended index, you can use hint to force an index. Such as:
> Db.c.find ({"Age": +, "username":/.*/}). Hint ({"username": 1, "Age": 1})
Like using hint in Oracle, hint is not necessarily specified in most cases. Because MongoDB is very smart, it will choose to use such an index for you.
Manage Indexes
Index meta information is in the System.indexes collection
> Db.system.indexes.find ()
{"V": 1, "key": {"_id": 1}, "name": "_id_", "ns": "Test.my_collection"}
{"V": 1, "key": {"X": 1}, "name": "X_1", "ns": "Test.my_collection"}
{"V": 1, "key": {"_id": 1}, "name": "_id_", "ns": "Test.people"}
......
{"V": 1, "key": {"_id": 1}, "name": "_id_", "ns": "Test.stocks"}
{"V": 1, "name": "Username_1", "key": {"username": 1}, "ns": "Test.users1"}
{"V": 1, "name": "_id_", "key": {"_id": 1}, "ns": "Test.bolg.posts"}
{"V": 1, "name": "Comments.date_1", "key": {"comments.date": 1}, "ns": "Test.bolg.posts"}
--Delete Index
> db.users1.dropIndexes ({"username": 1})
{
"Nindexeswas": 2,
"MSG": "non-_id indexes dropped for collection",
"OK": 1
}
--or use the RunCommand command to delete an index
> Db.runcommand ({"dropindexes": "Bolg.posts", "index": "Comments.date_1"})
{"Nindexeswas": 2, "OK": 1}
--Modify Index (rebuild index)
When the original index is not good, you need to rebuild the index, you can use the Backgroud option to make the creation process run in the background, which will prevent the database from blocking.
> Db.users1.ensureIndex ({username:1},{backgroud:true})
Geo-Spatial Index
Another query becomes more and more popular: to find the nearest n places to the current location, find the nearest café around the given latitude and longitude coordinates.
MongoDB provides a specialized index for the coordinate plane, called: Geospatial Index
Can also be created with Ensureindex, except that the parameter is not 1 or-1, but rather "2d"
Db.map.ensureIndex ({"GPs": "2d"})
"GPs" must be a pair of values in some form: an array of two elements or an inline document containing two keys, and the key value name can be arbitrary. As follows:
{"GPs": [0, 100]}
{"GPs": {"x": -30, "Y": 30}}
{"GPs": {"latitude": -180, "Longitude": 180}}
By default, the extent of the geospatial is -180~180 (latitude and longitude), and to use other values, you can specify the maximum minimum value with the ENSUREINDEX option:
> Db.star.trek.ensureIndex ({"Light-years": "2d"}, {"Min": -1000, "Max": 1000})
This creates an index of 2000 light-years square.
Geo-Spatial query methods
Using $near
Return 10 documents from [40,-73]
> Db.map.find ({"GPs": {"$near": [ -73]}}). Limit (10)
Or:
> Db.runcommand ({geonear: "Map", near: [ -73], num:10});
Finds documents within a specified beginning
Change the original $near to $within
$within shape parameter document (http://www.mongodb.org/display/DOCS/Geospatial+Indexing)
Rectangle: Using "$box"
> Db.map.find ({"GPs": {"$within": {"$box": [[10, 20], [15, 30]]}})
Circle: Use "$center"
> Db.map.find ({"GPs": {"$within": {"$center": [[12, 25], 5]}})
Composite Geospatial Index
It's not just a place to apply what you're often looking for. For example, the user wants to find all around the coffee shop or pizzeria. This requirement is met by combining a geospatial index with a normal index.
For example, to query "location" and "desc", you can create an index like this:
> Db.ensureindex ({"Location": "2d", "desc": 1})
And then it's possible to find the nearest café soon.
> Db.map.find ({"Location": {"$near": [ -70, +]}, "desc": "CoffeeShop"}). Limit (1)
{
"_id": ObjectId ("4c0d1348928a815a720a0000"),
"Name": "Mud",
"Location": [X, Y],
"desc": ["Coffee", "coffeeshop", "muffins", "espresso"]
}
Note: Creating a key phrase is helpful for user-defined lookups.
MongoDB Index Use