c# - 复制集合索引 c# mongo
问题描述
每个月我都需要更新我的 mongo 集合,我删除我的旧集合,然后在同一个数据库中手动创建具有相同索引的集合。然后我运行一个程序,将 txt 文件中的信息插入到集合中。我这样做的原因是因为集合很大,删除所有文档需要太多时间,删除它们要快得多。
由于有许多具有不同索引的集合,我希望以编程方式执行此操作。有没有办法在 C# 中以编程方式执行此操作?
- 将集合中的索引复制到列表中
- 丢弃集合
- 使用列表中的索引和相同的名称创建集合
其中一些集合具有 2DSPHERE 索引。
到目前为止,这是我的代码,但我陷入了从列表中创建索引的最后一部分:
//connecting to mongo
MongoClient client = new MongoClient("mongodb://" + MongoUser + ":" + MongoPass + "@" + MongoURL);
IMongoCollection<BsonDocument> myCollection = client.GetDatabase("DatabaseName").GetCollection<BsonDocument>("CollectionName");
//Save indexes into a list before drop
List<BsonDocument> indexList = myCollection.Indexes.List().ToList();
//Drop and recreate
client.GetDatabase("DatabaseName").DropCollection("CollectionName")
client.GetDatabase("DatabaseName").CreateCollection("CollectionName");
// Get new created collection
myCollection = client.GetDatabase("DatabaseName").GetCollection<BsonDocument>("CollectionName");
foreach (BsonDocument doc in indexList )
{
//Not working
myCollection.Indexes.CreateOne(doc);
}
解决方案
推荐阅读
- json - Swift JSON 编码问题
- python - 张量流数据集可以容纳的最大样本大小?
- ansible - Ansible 的“修复”状态,当配置文件中存在错误且未安装包时
- c++ - 法线贴图覆盖 DirectX 11 中的平滑边缘
- google-assistant-sdk - 谷歌动作测试控制台不允许我在模拟器中运行代码
- javascript - 如何根据选中的一个或多个复选框显示/隐藏 div
- javascript - 尽管声明,分配值并填充数组,但数组类型未定义?
- sql - 基于两列的枢轴
- wordpress - 我正在尝试在我的 wordpress 菜单中添加自定义图标标签
- pandas - 是否有另一种更有效的方法可以从 pandas 数据帧的多索引中取消堆叠列?