本文整理汇总了C#中MongoCollection.GetIndexes方法的典型用法代码示例。如果您正苦于以下问题:C# MongoCollection.GetIndexes方法的具体用法?C# MongoCollection.GetIndexes怎么用?C# MongoCollection.GetIndexes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MongoCollection
的用法示例。
在下文中一共展示了MongoCollection.GetIndexes方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Open
// 初始化
// parameters:
public int Open(
// string strMongoDbConnStr,
MongoClient client,
string strInstancePrefix,
out string strError)
{
strError = "";
#if NO
if (string.IsNullOrEmpty(strMongoDbConnStr) == true)
{
strError = "strMongoDbConnStr 参数值不应为空";
return -1;
}
#endif
if (string.IsNullOrEmpty(strInstancePrefix) == false)
strInstancePrefix = strInstancePrefix + "_";
_logDatabaseName = strInstancePrefix + "accessLog";
#if NO
try
{
this._mongoClient = new MongoClient(strMongoDbConnStr);
}
catch (Exception ex)
{
strError = "初始化 MongoClient 时出错: " + ex.Message;
return -1;
}
#endif
var server = client.GetServer();
{
var db = server.GetDatabase(this._logDatabaseName);
_logCollection = db.GetCollection<AccessLogItem>("accessLog");
// _logCollection.DropAllIndexes();
if (_logCollection.GetIndexes().Count == 0)
{
#if NO
_logCollection.CreateIndex(new IndexKeysBuilder().Ascending("OperTime"),
IndexOptions.SetUnique(false));
#endif
CreateIndex();
}
}
_itemCount = new DailyItemCount();
string date = GetToday();
_itemCount.SetValue(date, GetItemCount(date));
return 0;
}
示例2: MongoDbPessimisticLocker
internal MongoDbPessimisticLocker(MongoDatabase database, int ttl, bool dropCollection)
{
locks = database.GetCollection<PessimisticLockDto>("PessimisticLocks");
if (dropCollection)
locks.Drop();
var indexes = locks.GetIndexes().ToArray();
var tsIndex = indexes.FirstOrDefault(x => x.Name != "ts");
if (tsIndex == null)
{
locks.CreateIndex(new IndexKeysBuilder().Ascending("ts"),
new IndexOptionsBuilder().SetTimeToLive(TimeSpan.FromSeconds(ttl)).SetName("ts"));
}
locks.CreateIndex(new IndexKeysBuilder().Ascending("kid"), new IndexOptionsBuilder().SetUnique(false));
}
示例3: Open
// 初始化
// parameters:
public int Open(
// string strMongoDbConnStr,
MongoClient client,
string strInstancePrefix,
out string strError)
{
strError = "";
#if NO
if (string.IsNullOrEmpty(strMongoDbConnStr) == true)
{
strError = "strMongoDbConnStr 参数值不应为空";
return -1;
}
#endif
if (string.IsNullOrEmpty(strInstancePrefix) == false)
strInstancePrefix = strInstancePrefix + "_";
m_strHitCountDatabaseName = strInstancePrefix + "hitcount";
#if NO
try
{
this.m_mongoClient = new MongoClient(strMongoDbConnStr);
}
catch (Exception ex)
{
strError = "初始化 MongoClient 时出错: " + ex.Message;
return -1;
}
#endif
var server = client.GetServer();
{
var db = server.GetDatabase(this.m_strHitCountDatabaseName);
_hitCountCollection = db.GetCollection<HitCountItem>("hitcount");
if (_hitCountCollection.GetIndexes().Count == 0)
CreateIndex();
}
return 0;
}
示例4: CreateIndex
/// <summary>
/// 创建索引
/// </summary>
/// <param name="collection"></param>
/// <param name="v"></param>
private void CreateIndex(MongoCollection collection, Variant v)
{
var indexs = collection.GetIndexes();
HashSet<string> hs = new HashSet<string>();
foreach (IndexInfo index in indexs)
{
hs.Add(index.Name);
}
foreach (var item in v)
{
string indexName = item.Key;
Variant keys = item.Value as Variant;
if (keys != null)
{
IndexOptionsBuilder indexOpt = new IndexOptionsBuilder();
indexOpt.SetName(indexName);
if (keys.ContainsKey("dropDups"))
{
if (keys["dropDups"] is bool)
{
indexOpt.SetDropDups((bool)keys["dropDups"]);
}
keys.Remove("dropDups");
}
if (keys.ContainsKey("background"))
{
if (keys["background"] is bool)
{
indexOpt.SetBackground((bool)keys["background"]);
}
keys.Remove("background");
}
if (keys.ContainsKey("unique"))
{
if (keys["unique"] is bool)
{
indexOpt.SetUnique((bool)keys["unique"]);
}
keys.Remove("unique");
}
if (keys.ContainsKey("sparse"))
{
if (keys["sparse"] is bool)
{
indexOpt.SetSparse((bool)keys["sparse"]);
}
keys.Remove("sparse");
}
bool find = false;
IndexKeysBuilder indexKey = new IndexKeysBuilder();
foreach (var dsa in keys)
{
string ckey = dsa.Key;
if (ckey != string.Empty && dsa.Value is int)
{
int dec = (int)dsa.Value;
if (dec == 1)
{
indexKey.Ascending(ckey);
find = true;
}
else if (dec == -1)
{
indexKey.Descending(ckey);
find = true;
}
}
}
if (find)
{
if (!hs.Contains(indexName))
collection.CreateIndex(indexKey, indexOpt);
}
else
{
if (hs.Contains(indexName))
collection.DropIndexByName(indexName);
}
}
}
}