如何强制 MongoDB 使用 BasicCursor 而不是索引?
为避免使用索引,请在 MongoDB 中使用 hint()。让我们创建一个带有文档的集合 −
> 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")
}使用 find() 方法显示集合中的所有文档 −
> db.demo31.find();
这将生成以下输出 −
{ "_id" : ObjectId("5e174f8fcfb11e5c34d898c1"), "StudentFirstName" : "John" }
{ "_id" : ObjectId("5e174f97cfb11e5c34d898c2"), "StudentFirstName" : "Jace" }
{ "_id" : ObjectId("5e174f9ccfb11e5c34d898c3"), "StudentFirstName" : "Chris" }
{ "_id" : ObjectId("5e174fa0cfb11e5c34d898c4"), "StudentFirstName" : "James" }以下是对 BasicCursor 代替索引的 MongoDB 强制查询 −
> db.demo31.find({"StudentFirstName": {$regex: '^Ja'}}).hint({ $natural: 1});这将生成以下输出 −
{ "_id" : ObjectId("5e174f97cfb11e5c34d898c2"), "StudentFirstName" : "Jace" }
{ "_id" : ObjectId("5e174fa0cfb11e5c34d898c4"), "StudentFirstName" : "James" }
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP