Indexes
This commit is contained in:
parent
728b881ac1
commit
e3c43a68f7
66
README.md
66
README.md
@ -330,3 +330,69 @@ Returns:
|
||||
{ "_id" : "Charmander", "total" : 20 }
|
||||
```
|
||||
[More SQL vs Mongo examples](https://docs.mongodb.com/manual/reference/sql-aggregation-comparison/)
|
||||
|
||||
# Indexes for faster searching
|
||||
Get indexes of a collection:
|
||||
```
|
||||
> db.pokemons.getIndexes()
|
||||
```
|
||||
It will return `_id` because it's the index by default
|
||||
```
|
||||
[{
|
||||
"v" : 2,
|
||||
"key" : {
|
||||
"_id" : 1
|
||||
},
|
||||
"name" : "_id_"
|
||||
}]
|
||||
```
|
||||
|
||||
## Create an index
|
||||
Create type text index
|
||||
```
|
||||
> db.pokemons.createIndex({name: "text"})
|
||||
```
|
||||
Will return
|
||||
```
|
||||
{
|
||||
"numIndexesBefore" : 1,
|
||||
"numIndexesAfter" : 2,
|
||||
"createdCollectionAutomatically" : false,
|
||||
"ok" : 1
|
||||
}
|
||||
```
|
||||
And now we will get more indexes:
|
||||
```
|
||||
> db.pokemons.getIndexes()
|
||||
|
||||
[
|
||||
{
|
||||
"v" : 2,
|
||||
"key" : {
|
||||
"_id" : 1
|
||||
},
|
||||
"name" : "_id_"
|
||||
},
|
||||
{
|
||||
"v" : 2,
|
||||
"key" : {
|
||||
"_fts" : "text",
|
||||
"_ftsx" : 1
|
||||
},
|
||||
"name" : "name_text",
|
||||
"weights" : {
|
||||
"name" : 1
|
||||
},
|
||||
"default_language" : "english",
|
||||
"language_override" : "language",
|
||||
"textIndexVersion" : 3
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
## Searching by text
|
||||
```
|
||||
> db.pokemons.find({$text: {$search: "Pikachu"}})
|
||||
|
||||
{ "_id" : ObjectId("61422737f096f662d9ea826d"), "name" : "Charmander", "level" : 20, "pokedexId" : 2, "type" : [ "fire" ] }
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user