我们如何解决前.消息 =“无法将类型为'MongoDB.Bson.ObjectId'的对象转换为类型'MongoDB.B
本文关键字:类型 MongoDB Bson 转换 对象 ObjectId 解决 何解决 消息 我们 | 更新日期: 2023-09-27 18:17:29
All:
插入MongoDB数据库后,我无法将类型为"MongoDB.Bson.ObjectId"的对象转换为类型"MongoDB.Bson.BsonValue">
ELLCsInterfaceLogEvent aLogEvent = null;
try
{
MongoDB.Bson.ObjectId UmbrellaLogIdAsObjectId = (ObjectId)UmbrellaLogId;
BsonDocument logEventBsonDoc = new BsonDocument {
{"UmbrellaLogId",(BsonValue)UmbrellaLogIdAsObjectId}
}; // end of new BsonDocument
objUtility = new Utility();
objUtility.Insert(logEventBsonDoc, "FMS_TM_MST_LogEvents");
string[] arrFields = { "UmbrellaLogId" };
IMongoQuery query = Query.EQ("UserID", (BsonValue)UmbrellaLogId);
aLogEvent = DBConnection.database.GetCollection<ELLCsLogEvent>("FMS_TM_MST_LogEvents")
.Find(query).SetFields(arrFields).ToList<ELLCsInterfaceLogEvent>().FirstOrDefault();
}
catch (Exception ex)
{
string something = ex.Message;
}
如何轻松地在无法强制转换类型为"MongoDB.Bson.ObjectId"的对象到类型"MongoDB.Bson.BsonValue"之间转换?
对不起,误报。 这是一个愚蠢的编程错误。
在我的查询中检查我是否插入了条目,我未能进行转换。
这是正确的代码:
ELLCsInterfaceLogEvent aLogEvent = null;
try
{
//BsonValue bval = MongoDB.Bson.BsonValue((ObjectId)UmbrellaLogId);
//new MongoDB.Bson.BsonValue((ObjectId)UmbrellaLogId);
MongoDB.Bson.ObjectId UmbrellaLogIdAsObjectId = (ObjectId)UmbrellaLogId;
BsonDocument logEventBsonDoc = new BsonDocument {
{"UmbrellaLogId",(BsonValue)UmbrellaLogIdAsObjectId}
}; // end of new BsonDocument
objUtility = new Utility();
objUtility.Insert(logEventBsonDoc, "FMS_TM_MST_LogEvents");
string[] arrFields = { "UmbrellaLogId" };
IMongoQuery query = Query.EQ("UmbrellaLogId", (BsonValue)UmbrellaLogIdAsObjectId);
aLogEvent = DBConnection.database.GetCollection<ELLCsLogEvent>("FMS_TM_MST_LogEvents")
.Find(query).SetFields(arrFields).ToList<ELLCsInterfaceLogEvent>().FirstOrDefault();
}
catch (Exception ex)
{
string something = ex.Message;
}
return aLogEvent;