Để tạo chỉ mục trong nền, hãy sử dụng phương thức createIndex () và đặt “background:true” như trong cú pháp bên dưới -
db.yourCollectionName.createIndex({"yourFieldName1":1,"yourFieldName2":1},{background: true} ); Hãy để chúng tôi triển khai cú pháp trên để tạo chỉ mục và thiết lập nền -
> db.indexCreationDemo.createIndex({"StudentName":1,"StudentAge":1},{background: true} );
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
} Hãy để chúng tôi hiển thị các chỉ mục -
> db.indexCreationDemo.getIndexes();
Điều này sẽ tạo ra kết quả sau -
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "web.indexCreationDemo"
},
{
"v" : 2,
"key" : {
"StudentName" : 1,
"StudentAge" : 1
},
"name" : "StudentName_1_StudentAge_1",
"ns" : "web.indexCreationDemo",
"background" : true
}
]