本文整理汇总了C#中MongoCollection.Update方法的典型用法代码示例。如果您正苦于以下问题:C# MongoCollection.Update方法的具体用法?C# MongoCollection.Update怎么用?C# MongoCollection.Update使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MongoCollection
的用法示例。
在下文中一共展示了MongoCollection.Update方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Upsert
protected bool Upsert(MongoCollection<object> collection, object obj, Guid id)
{
if (collection.FindOneById(id) != null)
return collection.Update(Query.EQ("_id", id), Update.Replace(obj), WriteConcern.Acknowledged).Ok;
else
return Add(collection, obj);
}
示例2: DoRemove
SafeModeResult DoRemove(Type messageType, string subscriberInputQueue, MongoCollection<BsonDocument> collection)
{
return collection.Update(Query.EQ("_id", messageType.FullName),
Update.Pull("endpoints", subscriberInputQueue),
UpdateFlags.Upsert,
SafeMode.True);
}
示例3: NewMethod
private int NewMethod(MongoCollection coll, string fbname)
{
var v = coll.FindAs<BsonDocument>(Query.Null).SetFields(fbname);
Dictionary<BsonValue, string> results = new Dictionary<BsonValue, string>();
foreach (var item in v)
{
BsonValue value;
if (item.TryGetValue(fbname, out value))
{
string oldName = value.ToString();
string newName = ReplayceName(oldName);
if (oldName != newName)
{
item.TryGetValue("_id", out value);
results.Add(value, newName);
}
}
}
foreach (var item in results)
{
BsonValue id = item.Key;
string newName = item.Value;
try
{
Query.EQ("_id", id);
Update.Set(fbname, newName);
coll.Update(Query.EQ("_id", id), Update.Set(fbname, newName), SafeMode.False);
}
catch (System.Exception ex)
{
LogWrapper.Error("renameErr:" + id + " newName" + newName, ex);
}
}
return results.Count;
}
示例4: ClearMongoSessionObject
public void ClearMongoSessionObject(MongoCollection<SessionObject> collection, string sessionID) {
var query = Query<SessionObject>.EQ(x => x.BsonID, SessionObject.GetBsonId(this._applicationName, sessionID));
var update = Update<SessionObject>
.Set(x => x.IsLocked, false)
.Set(y => y.SessionData, string.Empty);
collection.Update(query, update);
}
示例5: ChangeTranslation
private static void ChangeTranslation(MongoCollection english)
{
Console.WriteLine("Enter word.");
string word = Console.ReadLine();
Console.WriteLine("Enter translation.");
string translation = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(word) && !string.IsNullOrWhiteSpace(translation))
{
var query = Query.And(Query.EQ("Name", word));
var filtered = english.FindAs<Word>(query);
var count = filtered.Count();
if (count > 0)
{
foreach (var item in filtered)
{
var update = Update.Set("Translation", translation);
var queryId = Query.EQ("_id", item.Id);
english.Update(queryId, update);
Console.WriteLine("Translation of the word {0} is changed.", word);
}
}
else
{
Console.WriteLine("There is no word {0}.", word);
}
}
else
{
Console.WriteLine("You enter null or empty string for word or translation. Please try again.");
}
}
示例6: UnlockMongoSessionObject
public void UnlockMongoSessionObject(MongoCollection<SessionObject> collection, string sessionID, int lockID) {
var query = Query.And(
Query<SessionObject>.EQ(x => x.BsonID, SessionObject.GetBsonId(this._applicationName, sessionID)),
Query<SessionObject>.EQ(y => y.LockID, lockID)
);
var update = Update<SessionObject>
.Set(x => x.IsLocked, false);
collection.Update(query, update);
}
示例7: Modify
private static void Modify(MongoCollection<Log> logs)
{
Console.WriteLine("UPDATE");
var query = Query.EQ("LogType.State", "pending");
var update = Update.Set("Text", "Changed to 'closed'").Set("LogType.State", "closed");
logs.Update(query, update, UpdateFlags.Multi);
logs.Find(Query.EQ("Text", "Changed to 'closed'")).Print();
}
示例8: Update
public int Update(MongoCollection<BsonDocument> collection, IDictionary<string, object> data, SimpleExpression criteria)
{
var condition = _expressionFormatter.Format(criteria);
MongoIdKeys.ReplaceId(data);
var update = new UpdateDocument("$set", data.ToBsonDocument());
var result = collection.Update(condition, update, UpdateFlags.Multi);
if (result != null)
return (int)result.DocumentsAffected;
return int.MaxValue;
}
示例9: LockSession
public bool LockSession(MongoCollection<SessionObject> collection, string sessionID) {
var query = Query.And(
Query<SessionObject>.EQ(x => x.BsonID, SessionObject.GetBsonId(this._applicationName, sessionID)),
Query<SessionObject>.EQ(y => y.IsLocked, false),
Query<SessionObject>.GT(z => z.ExpiresDate, DateTime.UtcNow)
);
var update = Update<SessionObject>
.Set(x => x.IsLocked, true)
.Set(y => y.LockedDate, DateTime.UtcNow)
.Inc(z => z.LockID, 1);
var result = collection.Update(query, update);
return result.DocumentsAffected > 0;
}
示例10: TmMq
protected TmMq()
{
ErrorCollection = GetCollection( "error" );
m_configCollection = GetCollection( "config" );
m_configCollection.Update( Query.EQ( "Type", "pubsub" ), Update.Set( "Type", "pubsub" ), UpdateFlags.Upsert, WriteConcern.Acknowledged );
//Find orphaned pub/sub collections
lock( g_pubsubUpdateSync )
{
g_pubsubUpdating = true;
try
{
foreach( var name in Db.GetCollectionNames() )
{
if( name.StartsWith( "pubsub_" ) )
{
int idxSubscriber = name.LastIndexOf( '_' );
if( idxSubscriber < 7 )
{
continue;
}
string queueName = name.Substring( 7, idxSubscriber - 7 );
string subscriberName = name.Substring( idxSubscriber + 1 );
ConcurrentDictionary<Guid, DateTime> subscribers;
if( !g_pubsubQueues.TryGetValue( queueName, out subscribers ) )
{
subscribers = new ConcurrentDictionary<Guid, DateTime>();
g_pubsubQueues[queueName] = subscribers;
}
subscribers[Guid.Parse( subscriberName )] = DateTime.MinValue;
}
}
}
finally
{
g_pubsubUpdating = false;
}
}
}
示例11: Update
public int Update(MongoCollection<BsonDocument> collection, IDictionary<string, object> data, SimpleExpression criteria)
{
var condition = _expressionFormatter.Format(criteria);
if (data.ContainsKey("Id"))
{
data["_id"] = data["Id"];
data.Remove("Id");
}
var update = new UpdateDocument("$set", data.ToBsonDocument());
var result = collection.Update(condition, update, UpdateFlags.Multi);
if (result != null)
return (int)result.DocumentsAffected;
return int.MaxValue;
}
示例12: EditWord
public static void EditWord(MongoCollection words)
{
Console.Write("Enter word for edit: ");
string editWord = Console.ReadLine().ToLower();
Console.Write("Enter new translation: ");
string translation = Console.ReadLine().ToLower();
var searched = words.AsQueryable<Word>().FirstOrDefault(w => w.Value == editWord);
if (searched != null)
{
var update = Update.Set("Translation", translation);
var query = Query.EQ("_id", searched.Id);
words.Update(query, update);
}
else
{
Console.WriteLine("This word is not exist");
}
}
示例13: UpdateUsingAtomicIncrement
/// <summary>
/// Demoes Updates a single field using an atomic Increment ($inc) operator.
/// </summary>
/// <param name="orders">The orders.</param>
private static void UpdateUsingAtomicIncrement(MongoCollection<Order> orders)
{
Console.WriteLine("\n\n======= Update Document using Increment =======");
var query = Query.EQ("CustomerName", "Daffy Duck");
Console.WriteLine("Before Update: " + orders.FindOne(query));
// Add 2000 to order amount on document matching selector.
orders.Update(query, Update.Inc( "OrderAmount", 2000 ));
Console.WriteLine("After Update: " + orders.FindOne(query));
}
示例14: ResetTimeoutMongoSessionObject
public void ResetTimeoutMongoSessionObject(MongoCollection<SessionObject> collection, string sessionID, double timeoutInMinutes) {
var query = Query<SessionObject>.EQ(x => x.BsonID, SessionObject.GetBsonId(this._applicationName, sessionID));
var update = Update<SessionObject>.Set(x => x.ExpiresDate, DateTime.UtcNow.AddMinutes(timeoutInMinutes));
collection.Update(query, update);
}
示例15: EditClick
static void EditClick(Click click, MongoCollection clicks)
{
Console.WriteLine("Enter new click name:");
string clickName = Console.ReadLine();
var update = Update.Set("Name", clickName);
var query = Query.EQ("_id", click.Id);
clicks.Update(query, update);
}