MongoDB study notes (6) MongoDB index usage and efficiency analysis

The indexes in MongoDB are actually similar to relational databases. They are all designed to improve the efficiency of query and sorting, and the implementation principles are basically the same. Because the keys (fields) in the collection can be ordinary data types or subdocuments. MongoDB can create indexes on various types of keys. The following explains the creation, query, and maintenance of various types of indexes. 1. Create index  1. Default index MongoDB has a default “_id” key, which is equivalent to the role of “primary key”. After the collection is created, the system will automatically create an index on the “_id” key. It is the default index. The index name is “_id_” and cannot be deleted. We can view it in the following ways: The code is as follows:var _idIndex = mongoCollection.Metadata.Indexes.Single(x => x.Key == “_id_”); Console.WriteLine(_idIndex); 2. Single column index An index created on a single key is a single-column index. For example, we want to create a single-column index for the “UserName” key on the “UserInfo” collection. The syntax is as follows: (1 means forward order, -1 means reverse order) The code is as follows: mongoCollection.Metadata.CreateIndex(new Document { { “UserName”, 1 } }, false); Next, we use…

MongoDB study notes (6) MongoDB index usage and efficiency analysis

1. Create an index 1. Default index MongoDB has a default “_id” key, which is equivalent to the role of “primary key”. After the collection is created, the system will automatically create an index on the “_id” key, which is the default index, and the index name is “_id_”, which cannot be deleted. We can check it with: var _idIndex = mongoCollection.Metadata.Indexes.Single(x => x.Key == “_id_”); Console.WriteLine(_idIndex); 2. Single column index The index created on a single key is a single-column index. For example, we want to create a single-column index for the “UserName” key on the “UserInfo” collection. The syntax is as follows: (1 indicates positive order, -1 reverse order) mongoCollection.Metadata.CreateIndex(new Document { { “UserName”, 1 } }, false); Next, we use the same method to find the index named “_UserName_” var _UserName_Index = mongoCollection.Metadata.Indexes.Single(x => x.Key == “_UserName_”); Console.WriteLine(_UserName_Index); 3. Composite index In addition, we can also create composite indexes for multiple keys at the same time. The following code creates a combined index according to the positive order of “UserId” and the reverse order of “UserName”: mongoCollection.Metadata.CreateIndex(new Document { { “UserId”, 1 }, { “UserName”, -1 } }, false); 4. Sub-document index We can create various indexes…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索