I have to Create a MongoDB connection and get records and update a single column value properly that working code.
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.
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.