[英]How to dynamic colums value update one by one on same id in mongodb using asp.net with c#
I have to Create a MongoDB connection and get records and update a single column value properly that working code.我必须创建一个 MongoDB 连接并获取记录并正确更新该工作代码的单列值。
public bool UpdateContactDetail(string ContactId,string ColumnsValue, BsonDocument document)
{
bool success = false;
try
{
MongoClient client = new MongoClient(ConnectionString);
MongoServer objServer = client.GetServer();
var DB = objServer.GetDatabase(DatabaseName);
var collection = DB.GetCollection(TableName);
var searchQuery = Query.EQ("_id", ObjectId.Parse(ContactId));
var sortBy = SortBy.Null;
update = Update.Set("Column Name", ColumnsValue);
collection.FindAndModify(searchQuery,sortBy, update);
success = true;
}
catch (MongoConnectionException) { return false; }
catch (Exception ex)
{
ExceptionHandling.InsertExceptionMessage("MongoDB", this.GetType().Name,
System.Reflection.MethodBase.GetCurrentMethod().Name, ex, 1);
return false;
}
}
I have added for a loop because I want to do multiple columns values update in the same record.我添加了一个循环,因为我想在同一条记录中更新多列值。 I do not want to give static columns names and why select id records deleted I don't understand.
我不想给出静态列名称以及为什么选择 id 记录被删除我不明白。
public bool UpdateContactDetail(string ContactId,string Taglist, BsonDocument document) // Columns Value and Taglist Updated
{
bool success = false;
try
{
MongoClient client = new MongoClient(ConnectionString);
MongoServer objServer = client.GetServer();
var DB = objServer.GetDatabase(DatabaseName);
var collection = DB.GetCollection(TableName);
var searchQuery = Query.EQ("_id", ObjectId.Parse(ContactId));
var sortBy = SortBy.Null;
**for (int i = 0; i < document.ElementCount; i++)
{
update = Update.Set(document.ToArray()[i].Name, document.ToArray()[i].Value.ToString());
collection.FindAndModify(searchQuery,sortBy, update);
}**
success = true;
}
catch (MongoConnectionException) { return false; }
catch (Exception ex)
{
ExceptionHandling.InsertExceptionMessage("MongoDB", this.GetType().Name, System.Reflection.MethodBase.GetCurrentMethod().Name, ex, 1);
return false;
}
}
Database and Collection Object should be initialized on every update.每次更新时都应初始化数据库和集合对象。 Try this code.
试试这个代码。 It's will work defiantly.
它会顽强地工作。
public bool UpdateContactDetail(string ContactId,string ColumnsValue, BsonDocument document)
{
bool success = false;
try
{
MongoClient client = new MongoClient(ConnectionString);
MongoServer objServer = client.GetServer();
for (int i = 0; i < document.ElementCount; i++)
{
var DB = objServer.GetDatabase(DatabaseName);
var collection = DB.GetCollection("EmailContacts");
var searchQuery = Query.EQ("_id", ObjectId.Parse(ContactId));
var sortBy = SortBy.Null;
var update = Update.Set(document.ToArray()[i].Name, document.ToArray()[i].Value.ToString());
collection.FindAndModify(searchQuery, sortBy, update);
}
success = true;
}
catch (MongoConnectionException) { return false; }
catch (Exception ex)
{
ExceptionHandling.InsertExceptionMessage("MongoDB", this.GetType().Name,
System.Reflection.MethodBase.GetCurrentMethod().Name, ex, 1);
return false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.