Unsupported conversion from object to objectId in $convert with no onError value

Hi guys,I need help to get information through the mongo query, but this information is inside a serialized java object.

My object have this content:

"details" : { "details" : "{ \"batteryLvl\": \"85\", \"operation\": \"DEVICE_STATUS_BATTERY\", \"doorSerial\": \"TLKJ3ACT53UTZTNW\", \"logInstant\": \"1682531305560\", \"class\": \"br.com.loopkey.model.logging.CommBatteryLoggingModel\" }"

I’m trying to convert this information into a string in several ways, using toString, or convert but I’m not succeeding. Because from there I would break information that I need.

When I try to convert using toString function:

> db.logs.aggregate([ { $match: { $and: [ { "doorId": 12804 }, { "operation": 130 } ] } }, { "$project": { "doorId": "$doorId", "details": "$details","teste":{$toString: "$details"}} }])
2023-04-27T00:59:03.603+0000 E QUERY    [js] Error: command failed: {
	"ok" : 0,
	"errmsg" : "Unsupported conversion from object to string in $convert with no onError value",
	"code" : 241,
	"codeName" : "ConversionFailure"
} : aggregate failed :

The version of mongo is 4.0.6. At first I have no way to update this mongo, so I would like to know if there is any way I can force this conversion to be able to treat it as a string?

Hi @Filipe_Carvalhedo and welcome to MongoDB community forums!!

I believe you’re seeing the effect of SERVER-46079, which is a known issue. And it’s not supported in the latest 6.0 version either, but it’s likely to be resolved in a future release.

In the meantime, we recommend upgrading to the latest supported version (refer to the Legacy Support Policy), as it contains the latest upgrades and bug fixes.

That being said, we can try to help you with a workaround, which may or may not be suitable for your use case. To do so, could you please provide us with the sample document and the expected output?


Hi @Aasawari, thanks for the answer. I’m using metabase software with mongo db. I need to extract the battery level of the details field. The details field have this content:

""details" : { "details" : "{ \"batteryLvl\": \"85\", \"operation\": \"DEVICE_STATUS_BATTERY\", \"doorSerial\": \"TLKJ3ACT53UTZTNW\", \"logInstant\": \"1682531305560\", \"class\": \"br.com.loopkey.model.logging.CommBatteryLoggingModel\" }""

I need to extract the batteryLvl infomartion, in this case is 85.

I know how to extract this information when the data is a string, but in this case is an object that I can’t convert to string.