我们如何解决ex.Message = "Unable to cast object of type 'MongoDB.Bson.ObjectId' to type 'MongoDB.Bson.BsonValue'."?
How do we resolve ex.Message = "Unable to cast object of type 'MongoDB.Bson.ObjectId' to type 'MongoDB.Bson.BsonValue'."?
全部:
插入 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;
全部:
插入 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;