इस मामले में, किसी विशेष फील्ड पर इंडेक्स की अवधारणा का उपयोग करें। पहले डॉक्यूमेंट्स के साथ एक कलेक्शन बनाएं। यहां हमने createIndex() का उपयोग करके इंडेक्स भी बनाया है:
> db.decreasetimeusingindex.createIndex({"StudentName":1});
{ "createdCollectionAutomatically": true, "numIndexesBefore": 1, "numIndexesAfter": 2, "ok": 1 }
> db.decreasetimeusingindex.insertOne({"StudentName":"John Smith","StudentAge":21});
{ "acknowledged": true, "insertedId": ObjectId("5e03960af5e889d7a51994ff") }
> db.decreasetimeusingindex.insertOne({"StudentName":"John Doe","StudentAge":24});
{ "acknowledged": true, "insertedId": ObjectId("5e039611f5e889d7a5199500") }
> db.decreasetimeusingindex.insertOne({"StudentName":"Adam Smith","StudentAge":22});
{ "acknowledged": true, "insertedId": ObjectId("5e03961df5e889d7a5199501") }
सभी डॉक्यूमेंट्स देखें:
> db.decreasetimeusingindex.find().pretty();
आउटपुट:
विशिष्ट रिकॉर्ड खोजने की क्वेरी:
> db.decreasetimeusingindex.find({"StudentName":"Adam Smith"});परिणाम: