mongodb index

1 Where to use the index

Everything that involves finding a place needs an index.

2 Types of mongodb indexes

2.1 Single field index

Same as mysql.

db.person.ensureIndex({age:1});

2.2 Combined Index

Same as mysql.

db.person.ensureIndex({age:1, name:1});

2.3 Multi-key index

When the indexed field is an array, mongodb creates an index for each element in the array.

{"name" : "jack", "age" : 19, habbit: ["football, runnning"]}
db.person.ensureIndex( {habbit: 1} )  // 自动创建多key索引
db.person.find( {habbit: "football"} )

2.4 Text Index

Full text index.

3 The condition of mongodb query is also a json object

db.user.find({"name":{"$gte":18, "$lte":20}});

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324689406&siteId=291194637