简体   繁体   English

Mongo Java驱动程序:收集删除后的娱乐索引

[英]Mongo Java-driver: recreation indexes after collection dropping

I have a necessity to remove all data from mongo collection. 我有必要从mongo集合中删除所有数据。 Droping of collection works faster than collection.remove(new BasicDBObject()) . 删除collection的速度比collection.remove(new BasicDBObject())更快。 But it also removes index definitions. 但是它也删除了索引定义。 Therefore I want to restore index definitions after collection dropping. 因此,我想在删除集合后恢复索引定义。

In general I have following plan: 总的来说,我有以下计划:

  1. Get all index definitions before dropping: http://www.mongodb.org/display/DOCS/Java+Tutorial#JavaTutorial-GettingaListofIndexesonaCollection 在删除之前获取所有索引定义: http : //www.mongodb.org/display/DOCS/Java+Tutorial#JavaTutorial-GettingaListofIndexesonaCollection
  2. Recreate definitions after dropping: http://www.mongodb.org/display/DOCS/Java+Tutorial#JavaTutorial-CreatingAnIndex 删除后重新创建定义: http : //www.mongodb.org/display/DOCS/Java+Tutorial#JavaTutorial-CreatingAnIndex

The issue for me is exact value that should be retrieved/passed for index recreation. 对我来说,问题是应检索/传递索引重新创建的确切值。 Eg will following correct work for all types of indexes: 例如,将对所有类型的索引执行以下正确工作:

List<DBObject> storedDefinitions = coll.getIndexInfo();
coll.drop();
for (DBObject storedDefinition : storedDefinitions) {
  coll.createIndex(storedDefinition.get("key"));
}

If no - what is the best way to achieve my goals? 如果没有-实现目标的最佳方法是什么?

Thanks for any help! 谢谢你的帮助!

You're close. 你近了 You will need to read the index options of the retrieved indexes and apply those to the newly created index using the createIndex(DBObject key, DBObject options) version of the method. 您将需要读取检索到的索引的索引选项,然后使用该方法的createIndex(DBObject key,DBObject options)版本将其应用于新创建的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM