Full-Text Indexing
Build method:--Create a full-text index on the key field of the articles collection Db.articles.ensureIndex ({key: "text"})--Key_1,key_ in the articles collection 2 Create full-text index on field db.articles.ensuereIndex ({key_1: "text", Key_2: "Text"})-- Create a full-text index on all fields of the articles collection Db.articles.ensuereIndex ({"$**": "Text"})--Create a full-text index on the article field in the Articles collection > Db.articles.ensureIndex ({"article": "Text"}); {"createdcollectionautomatically": True, "Numindexesbefore": 1, "Numindexesafter": 2, "OK" : 1}>--Insert demo data Db.articles.insert ({"article": "AA BB"}) Db.articles.insert ({"article": "AA bb cc"}) Db.articles.insert ({"article": "AA bb cc DD"}) Db.articles.insert ({"article": "AA bb cc RR"}) use full-text index query--query that contains AA or BB or CC's document Db.articles.find ({$text: {$search: "AA bb cc"}})--Query the document Db.articles.find ({$text: {$search: "AA bb-cc) that contains AA or BB but does not contain CC "}})--Query the document Db.articles.find ({$text: {$search:" \ "aa\" \ "bb\" \ "Cc\"}}) containing the AA, BB, and CC containing the similarity $meta operator: {score:{$meta: "Textscore"}} The similarity of the query results that can be returned after the query condition is often used with sort-queries the text containing the "AA bb" field and returns the similarity of the query results > db.articles.find ({$text: {$search: "AA BB"}},{score:{$mETA: "Textscore"}); {"_id": ObjectId ("5798ad223206da9bc38b2370"), "article": "AA bb cc DD", "Score": 1.25} {"_id": ObjectId ("5798ad293206da9bc38b2371"), "article": "AA bb cc RR", "Score": 1.25} {"_id": ObjectId ("5798ad1b3206da9bc38b236f"), "article": "AA bb cc", "Score": 1.3333333333333333} {"_id": ObjectId ("5798ad143206da9bc38b236e"), "article": "AA BB", "Score": 1.5} {"_id": ObjectId ("5798ae383206da9bc38b2372"), "article": "AA", "score": 1.1}--Sort by similarity > Db.articles.find ({$text: {$ Search: "AA bb"}},{score:{$meta: "Textscore"}). Sort ({score:{$meta: "Textscore"}}); {"_id": ObjectId ("5798ad143206da9bc38b236e"), "article": "AA BB", "Score": 1.5} {"_id": ObjectId ("5798ad1b3206da9bc38b236f"), "article": "AA bb cc", "Score": 1.3333333333333333} {"_id": ObjectId ("5798ad223206da9bc38b2370"), "article": "AA bb cc DD", "Score": 1.25} {"_id": ObjectId ("5798ad293206da9bc38b2371"), "article": "AA bb cc RR", "Score": 1.25} {"_id": ObjectId ("5798ae383206da9bc38b23"article": "AA", "score": 1.1}> restrictions used by full-text indexing (version 2.6.5) 1. You can specify only one $text query per query (a collection can only create one full-text index) 2. $text query cannot appear in the nor query in 3. Query if the containing $text,hint does not function 4. The current MongoDB full-text index does not support Chinese
Indexed properties
Format for creating an index: Db.collection.ensureIndex ({indexvalue},{indexproperty}) where Indexproperty is more important: 1. Name, designation: Db.collection.ensureIndex ({},{name: ""}) 2. Uniqueness, unique designation: db.collection.ensureIndex ({},{unique:true/false}) 3. sparsity, sparse specifies whether to create an index Db.collection.ensureIndex ({},{sparse:true/false}) for a field that does not exist in the document 4. Whether to delete periodically, Expireafterseconds Specify: TTL expiration Index
Location Index
Geo-index stores the location of some points in MongoDB, after the index is created, You can find other sub-categories by Location 2d index: used to store and find points on a plane 2dsphere index: used to store and find points on a spherical surface find the way 1. Find points within a certain distance of a point 2. Find a point within a range 2d index detailed 1. To create an index Db.collection.ensureIndex ({w: "2d"}) 2. Location representation latitude and longitude [longitude, latitude]3. Range of longitude [-180,180] latitude [ -90,90]4. Insert location data > Db.location.insert ({w:[1,1]}) >db.location.insert ({w:[1,2]}) >db.location.insert ({w:[5,6]}) > Db.location.insert ({w:[200,1]}) #Mongodb会直接报错, longitude out of range >db.location.insert ({w:[180,100]}) #纬度超出范围, MongoDB does not have an error, However, late queries can cause unpredictable errors. >db.location.insert ({w:[79,76]}) 5. Query method $near query query from the nearest point of a point >db.location.find ({w:{$near: [[]}}) #默认返回100个- -$maxdistance can be used to limit the maximum distance found >db.location.find ({w:{$near: [Max], $maxDistance: ten}) #限制查找最远距离为10 $geoWithin query There are three ways to query the shape of a point within a shape 1) $box: Rectangle, using {$box: [[<X1>,<Y1>],[<X2>,<y2>]} indicates that the interior is two coordinates, the first one represents the left boundary, and the second represents the right boundary. >db.collection.find ({w:{$geoWithin: {$box: [[[0,0],[3,3]]}}}) 2) $center: round, using {$center: [[<X1>,<Y1>],r]} indicates that the inner is the center position and radius. >db.collection.find ({w:{$geoWithin: {$center: [[[0,0],5]}}}] 3) $polygon: Polygon, using {$polygon: [[<X1>,<Y1>],[<X2>,<y2>],[<X3>,<Y3>]} indicates that the interior is a coordinate point, and the punctuation is surrounded by a polygon. >db.collection.find ({w:{$geoWithin: {$polygon: [[[0,0],[0,1],[2,5],[6,1]]}}) geonear query is a complement to $near queries Db.runcommand ({getnear:<Collection>, Near:[x,y], Mindistance: (Invalid for 2d index) Maxdistance:num:})
MongoDB Index-Learning notes (not finished)