Cursors
A cursor is a line of data that can be manipulated in a row, very similar to ResultSet data processing. In the MongoDB database, the control of the cursor is very simple, only need to use the Find () function to return the cursor.
for the returned cursor, to operate, use the following two functions: determine if there is a row of data: Hasnest () take out the current data: Next ()
For example:
var cursor = Db.students.find ();
Cursor.hasnext ();
Cursor.next ();
The above is the operation of the cursor, but it is not actually possible to use this, because the loop must be used to output the content.
Example: writing a specific operation code
var cursor = Db.students.find ();
while (Cursor.hasnext ()) {
var doc = Cursor.next ();
Print (doc.name);
}
The equivalent of each data is taken out individually for progressive control. When the cursor data is fetched, in fact each row of data returned is an object type of content, then if you need to data in the form of JSON, you can use the Printjson () function to complete. The code is as follows:
var cursor = Db.students.find ();
while (Cursor.hasnext ()) {
var doc = Cursor.next ();
Printjson (Doc.name);
}
In all known databases, only MongoDB cursor operations are the simplest and most intuitive. Index
In any database, indexes are a means to improve the performance of database retrieval, which is also present in the MongoDB database.
MongoDB index creation is divided into two types: auto-creation and manual creation.
Write code here
At this point the students collection does not go to set any index, through Getindexes ()
Example: Querying the indexed contents of a students collection in the default state
Db.students.getIndexes ();
If you create your own index, the syntax is as follows: index creation: DB. Collection name. Ensureindex ({"column": 1})
Set 1 o'clock to indicate that the index will be arranged in ascending order-1, indicating that the index will be arranged in a descending manner. Unique index
The primary purpose of a unique index is to use a field so that the contents of the field are not duplicated.
Example: Creating a unique index
Db.students.ensure ({"Name": 1}, {"Unique": true});
Expired Index
In some programs the site will appear several seconds after the information is deleted. For example: Mobile phone information Verification code. Outdated indexes can be easily implemented in MongoDB, but this time is often not accurate (this feature is very helpful when you are saving some temporary data).
Example: Setting an expiration index in a phones collection
Db.phones.ensureIndex ({"Time": 1}, {"Expireafterseconds:10"})//SET index expires after 10 seconds
Full-Text indexing
In some information management platform often need to do information fuzzy query, the earliest time is to take advantage of a field implementation of some kind of query, but this time the information returned is not very accurate, because can only query A field or B field, and in MongoDB implementation of a very simple full-text retrieval.
Example: Defining a new collection
Db.news.insert ({"title": "Mldn Mldnjava lxh gyh", "Content": "Gyh"})
Db.news.insert ({"title": "Mldn mldnjava lxh", " Content ":" java "})
Db.news.insert ({" title ":" Gyh "," Content ":" Sfq "})
Db.news.insert ({" title ":" Gyh "," Content ":" Gry "})
Db.news.insert ({" title ":" Sfq "," Content ":" Gry "})
Example: Setting up full-text indexing
Cond
the "$text" specifier is used to represent full-text indexing.
the "$search" operator is used for data queries using the following: query for the specified keyword: {"$search": "Query keyword"} queries multiple keywords (i.e. "or" relationships): {"$search": "Query keyword query keyword ..."} Query multiple keywords (i.e. "and" relationships): {"$search": "\" query keywords \ "\" Query keywords \ "..."} Query multiple keywords (exclude one): {"$search": "Query keyword query keyword ...-exclude keywords"}
Example: Querying a single content
Db.news.find ({"$text": {"$search": "Gry"}});
Example: Contains information about "Gry" and "SFQ"
Db.news.find ({"$text": {"$search": "Gry Gyh"}});
Example: Contains both "MLDN" and "lxh" content
Db.news.find ({"$text": {"$search": "\" mldn\ "\" Lxh\ ""}});
Example: Contains content with "MLDN" but no "gyh"
Db.news.find ({"$text": {"$search": "\" mldn\ "\" lxh\ "-gyh"}})
but in the full-text search operation, you can also use the similarity score to judge the results of the search.
Example: Scoring a result
Db.news.find ({"text": {"$search": "Gyh"}}, {"score": {"$meta": "Textscore"}})
Db.news.find ({"text": {"$search": " Gyh "}}, {" score ": {" $meta ":" Textscore "}})
By grading the scores, you can actually achieve more accurate information retrieval.
when the fields are too long, you can set up full-text indexing for all the fields.
Example: Setting up full-text indexing for all fields
Db.news.ensure ({"$**": "Text"});
Geographic Information Index
The Geographic Information index is divided into two categories: 2D Planar index and 2DSphere spherical index
In the 2D plane index is basically able to save the information are coordinates, and coordinates are preserved is the latitude and longitude coordinates.
Example: Defining a collection of shops
Db.shop.insert ({loc: [ten]});
Db.shop.insert ({loc: [One, Ten]});
Db.shop.insert ({loc: [Ten, One]});
Db.shop.insert ({loc: [n]});
Db.shop.insert ({loc: [+]});
Db.shop.insert ({loc: [+]});
Db.shop.insert ({loc: [120, 130]});
Cond