
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Force MongoDB to Use BasicCursor Instead of Index
To avoid using index, use hint() in MongoDB. Let us create a collection with documents −
> db.demo31.createIndex({"StudentFirstName":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo31.insertOne({"StudentFirstName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5e174f8fcfb11e5c34d898c1") } > db.demo31.insertOne({"StudentFirstName":"Jace"}); { "acknowledged" : true, "insertedId" : ObjectId("5e174f97cfb11e5c34d898c2") } > db.demo31.insertOne({"StudentFirstName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e174f9ccfb11e5c34d898c3") } > db.demo31.insertOne({"StudentFirstName":"James"}); { "acknowledged" : true, "insertedId" : ObjectId("5e174fa0cfb11e5c34d898c4") }
Display all documents from a collection with the help of find() method −
> db.demo31.find();
This will produce the following output −
{ "_id" : ObjectId("5e174f8fcfb11e5c34d898c1"), "StudentFirstName" : "John" } { "_id" : ObjectId("5e174f97cfb11e5c34d898c2"), "StudentFirstName" : "Jace" } { "_id" : ObjectId("5e174f9ccfb11e5c34d898c3"), "StudentFirstName" : "Chris" } { "_id" : ObjectId("5e174fa0cfb11e5c34d898c4"), "StudentFirstName" : "James" }
Following is the query to force MongoDB to use BasicCursor instead of an index −
> db.demo31.find({"StudentFirstName": {$regex: '^Ja'}}).hint({ $natural: 1});
This will produce the following output −
{ "_id" : ObjectId("5e174f97cfb11e5c34d898c2"), "StudentFirstName" : "Jace" } { "_id" : ObjectId("5e174fa0cfb11e5c34d898c4"), "StudentFirstName" : "James" }
Advertisements