Tag Archives: MongoDB Error

[Solved] “errmsg” : “Got invalid BSON from external server while reading from cursor

db.copyDatabase Error:

"errmsg" : "Got invalid BSON from external server while reading from cursor
:: caused by :: InvalidBSON: Cannot use decimal BSON type when the featureCompatibilityVersion is 3.2.
See http://dochub.mongodb.org/core/3.4-feature-compatibility."

This is because the featureCompatibilityVersion version is not correct
1. Check the current featureCompatibilityVersion version and
execute the command line db.adminCommand({getParameter:1,featureCompatibilityVersion:1})
to return { “featureCompatibilityVersion” : “3.2”, “ok ” : 1 }
2. Modify the featureCompatibilityVersion version
Execute the command line db.adminCommand ({setFeatureCompatibilityVersion: “3.4”})
to return { “featureCompatibilityVersion” : “3.4”, “ok” :1 } , after the modification is successful,
execute db.copyDatabase and it will be ok

[Solved] MongoDB Error: Command failed with error 251 (NoSuchTransaction)

Recently, I encountered an online bug. Accessing a specific interface will cause occasional exceptions. After checking the log, it is found that an error is reported during the operation of mongodb. The error information is as follows:

error message: Command failed with error 251 (NoSuchTransaction): 'Given transaction number 115 does not match any in-progress transactions. The active transaction number is 114' on server xx.xx.xx.xx:xxxx. The full response is {"errorLabels": ["TransientTransactionError"], "ok": 0.0, "errmsg": "Given transaction number 115 does not match any in-progress transactions. The active transaction number is 114", "code": 251, "codeName": "NoSuchTransaction"}

Through searching and troubleshooting, the problem was located to be due to processing MongoDB operations where two requests in the same transaction are sent to the DB at the same time, with the probability of generating the following scenario.

1. request 1 and request 2 are sent to Mongo at the same time and start execution
2. Request 1 is still executing and request 2 has completed
3. Since request 1 is not yet completed, the transaction has not really started at DB level, so request 2 cannot end normally (that is why the error states that transaction id 115 cannot be found, because the transaction is not yet registered in DB), resulting in transaction rollback and throwing exceptions
4. request 1 execution is complete, but the transaction has been rolled back, the operation is invalid
Reviewing the code, we found that the reason why two requests are sent to DB at the same time is that the zipWith() method is used for data merging. The feature of this method is that it will request two data to be merged to the database at the same time, which will trigger the aforementioned problem when operating Mongo.

The solution is very simple, just use the zipWhen() method instead. zipWhen will block and wait for the first data requested to arrive before requesting the second data, which perfectly circumvents this problem.

[Solved] MongoDB Error: FaileError: dToParse: Password must be URL Encoded for mongodb:// URL:

Mongodb – error failedtoparse: password must be URL encoded for mongodb:// URL:

Problem background:
when you log in to mongodb with @ password, an error is reported. Failedtoparse: password must be URL encoded for mongodb:// URL:

Problem analysis:
official: https://docs.mongodb.com/manual/reference/connection-string/#examples

If the username or password includes the following characters:

@ is %40

Solution:
we can replace the login password @ with %40.

After creating an Iam ordinary user, you can execute the following commands to log in to mongodb through the Iam user:

$ mongo --quiet mongodb://iam:'aaa%40intah'@127.0.0.1:27017/iam_analytics?authSource=iam_analytics

Mongodb error: authentication failed [How to Solve]

Error Messages:

Database connect error:

com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName=‘root’, source=‘gateway’, password=, mechanismProperties=} Command failed with error 18 (AuthenticationFailed): ‘Authentication failed.’ on server 192.168.81.13:27017. The full response is {“ok”: 0.0, “errmsg”: “Authentication failed.”, “code”: 18, “codeName”: “AuthenticationFailed”}.

 

Solution:

Connect URL

mongodb://user:password@host:port/dbname

Add parameter authsource

For example:

mongodb://user:password@host:port/dbname?authSource=admin

In general, authsource is admin and can be replaced as needed

[Solved] MongoDB Error: TypeError: Object of type ObjectId is not JSON serializable

Scene description

After the MongoDB database is used to insert data, an error is reported when the flash restful interface is used to return data:

TypeError: Object of type ObjectId is not JSON serializable

Cause analysis

The reason for this problem is that when data is written to MongoDB, even if you do not specify a _id field, a field will be automatically generated for each piece of data _id, for example:

{
    "_id" : ObjectId("6180af3ef261f0827ea248d6"),
    "省份名称" : "澳门",
    "省份链接" : "https://www.gongkaoleida.com/area/3509",
    "省份代号" : "3509"
}

This field is of ObjectId type and cannot be returned using JSON serialization, so an error will be reported

Solution:

Since _id field cannot be converted and has no effect on our actual data. Then pop it up after inserting the data. An example is as follows:

self.db.filter_result.insert_one(item)
item.pop('_id')