本文整理匯總了C#中BsonDocument.EnsurePath方法的典型用法代碼示例。如果您正苦於以下問題:C# BsonDocument.EnsurePath方法的具體用法?C# BsonDocument.EnsurePath怎麽用?C# BsonDocument.EnsurePath使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類BsonDocument
的用法示例。
在下文中一共展示了BsonDocument.EnsurePath方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Set
UpdateCompiler Set(BsonDocument document, string name, BsonValue value)
{
var r = document.EnsurePath(name);
if (r.Document != null)
{
r.Document[r.Key] = value;
}
else if (!r.Array.InsertOutOfRange(r.Index, () => value))
{
r.Array[r.Index] = value;
}
return this;
}
示例2: Rename
UpdateCompiler Rename(BsonDocument document, string name, string newName)
{
var r1 = document.ResolvePath(name, true); //_131028_234439 Weird, we can rename in arrays but follow Mongo that cannot.
if (r1 != null)
{
BsonValue value;
if (r1.Document.TryGetValue(r1.Key, out value))
{
var r2 = document.EnsurePath(newName);
r1.Document.Remove(r1.Key);
r2.Document[r2.Key] = value;
}
}
return this;
}
示例3: AddToSet
UpdateCompiler AddToSet(BsonDocument document, string name, BsonValue value, bool each)
{
var r = document.EnsurePath(name);
BsonValue vArray;
if (r.Array != null)
{
if (r.Array.InsertOutOfRange(r.Index, () => new BsonArray() { value }))
return this;
vArray = r.Array[r.Index];
}
else if (!r.Document.TryGetValue(r.Key, out vArray))
{
r.Document.Add(r.Key, new BsonArray() { value });
return this;
}
if (vArray.BsonType != BsonType.Array)
throw new InvalidOperationException(string.Format(null, @"Value ""{0}"" must be array.", name));
if (each && value.BsonType == BsonType.Array)
vArray.AsBsonArray.AddToSetEach(value.AsBsonArray);
else
vArray.AsBsonArray.AddToSet(value);
return this;
}
示例4: Inc
UpdateCompiler Inc(BsonDocument document, string name, BsonValue value)
{
var r = document.EnsurePath(name);
BsonValue old;
if (r.Document != null)
{
if (r.Document.TryGetValue(r.Key, out old))
{
if (!old.IsNumeric)
throw new MongoException(string.Format(null, @"Field ""{0}"" must be numeric.", name));
value = MyValue.Add(old, value);
}
r.Document[r.Key] = value;
}
else if (!r.Array.InsertOutOfRange(r.Index, () => value))
{
old = r.Array[r.Index];
if (!old.IsNumeric)
throw new MongoException(string.Format(null, @"Item ""{0}"" must be numeric.", name));
r.Array[r.Index] = MyValue.Add(old, value);
}
return this;
}
示例5: Push
static void Push(BsonDocument document, string name, BsonValue value, bool all)
{
var r = document.EnsurePath(name);
BsonValue v2 = BsonNull.Value;
if (r.Array != null)
{
if (!r.Array.InsertOutOfRange(r.Index, () => v2 = new BsonArray()))
v2 = r.Array[r.Index];
}
else if (!r.Document.TryGetValue(r.Key, out v2))
{
v2 = new BsonArray();
r.Document.Add(r.Key, v2);
}
if (v2.BsonType != BsonType.Array)
throw new InvalidOperationException(string.Format(null, @"Value ""{0}"" must be array.", name));
var array = v2.AsBsonArray;
if (all)
array.AddRange(value.AsBsonArray);
else
array.Add(value);
}