62,074
社区成员
发帖
与我相关
我的任务
分享
public static WriteConcernResult UpdateObjectWithSet<T>(this MongoCollection collection, T document, MongoUpdateOptions update, bool ignoreDefault = false)
{
var doc = document.ToBsonDocument();
var rules = new List<IMongoUpdate>();
BsonValue idValue = null;
foreach (var x in doc)
{
if (x.Name == "_id")
idValue = x.Value;
else if (!ignoreDefault && x.Value != BsonNull.Value)
rules.Add(Update.Set(x.Name, x.Value));
}
Debug.Assert(idValue != null);
return collection.Update(Query.EQ("_id", idValue), Update.Combine(rules), update);
}
public static WriteConcernResult UpdateObjectWithSet<T>(this MongoCollection collection, T document, MongoUpdateOptions update)
{
var doc = document.ToBsonDocument();
var rules = new List<IMongoUpdate>();
BsonValue idValue = null;
foreach (var x in doc)
{
if (x.Name == "_id")
idValue = x.Value;
else
rules.Add(Update.Set(x.Name, x.Value));
}
Debug.Assert(idValue != null);
return collection.Update(Query.EQ("_id", idValue), Update.Combine(rules), update);
}
public static WriteConcernResult UpdateObjectWithSet<T>(this MongoCollection collection, T document)
{
return UpdateObjectWithSet(collection, document, new MongoUpdateOptions { Flags = UpdateFlags.Upsert });
}
public static WriteConcernResult UpdateObjectWithSet<T>(this MongoCollection collection, T document, MongoUpdateOptions update)
{
var doc = document.ToBsonDocument();
var rules = new List<IMongoUpdate>();
var idValue = doc["_id"];
foreach (var x in doc)
{
if (x.Name == "_id")
idValue = x.Value;
else
rules.Add(Update.Set(x.Name, x.Value));
}
return collection.Update(Query.EQ("_id", idValue), Update.Combine(rules), update);
}
}