我很想在mongodb索引我想问 如果我在mongo shell中这样做
db.myCollection.ensureIndex({"Email":1})
它成功创建索引,但如果再次运行此命令,mongo shell将显示此消息
{ "numIndexesBefore" : 2, "note" : "all indexes already exist", "ok" : 1 }
当我在我的scala / java代码
中执行相同操作时,这是理想的情况class Test {
def myFunction= {
var index=collection.ensureIndex(new BasicDBObject("Email":1))
}
}
当我调用这个函数两次时,它不会抛出任何异常,因为mongo shell会index already exist
,所以我想知道这个命令是什么
collection.ensureIndex(new BasicDBObject("Email":1))
}
当我们再次打电话时再做
object Demo extends App
{
var t=Test()
t.myFunction
t.myFunction//what happends here ? what does this ensureIndex command do
}
请指导我谢谢
答案 0 :(得分:1)
根据documentation ensureIndex
在索引尚不存在的情况下在指定字段上创建索引,因此后续调用除了检查索引是否已存在外什么都不做。
答案 1 :(得分:1)
如上面的答案所示,多次通话都没有做任何事情。
你没有得到任何异常,因为当你第二次或第三次调用EnsureIndex时MongoDB没有抛出异常,它只是给你一个“索引已存在”的JSON。
// file: index.php
require 'vendor/autoload.php';
$someInstance = new App\CurrencyManager();
与第一次返回JSON或添加其他索引的方式相同。
Class WPSE_Submit_From_Front {
public $post_id;
public function set_post_id()
{
$this->post_id = $POST['id'];
}
}
}