Today, when I was using Mongoexport to export data meeting certain conditions, I encountered an error. Now I record it and further study the digital type of MongoDB for this error.

Today, we received a business request to export data from the order collection of the MongoDB database that meets the following criteria:

Db.qqwj_order.find ({“Source”:NumberInt(“21″),”Batch”:” Batch”:” order notification in payment: 2018/9/5″,”MsgContent”:/ not yet finished online payment /})

The MongoDB client tool NoSQLBooster for MongoDB is used to check the statement execution. The number of records is 15265.

Use mongoexport command to export data, execute command as follows:

/ data/mongo/mongobin344 / bin/mongoexport – h 172 X.X.X XX – port port – db database – u account – p – authenticationDatabase authentication database ‘password’ –type= CSV -c qqwj_order -f MsgContent,REC_CreateTime -q ‘{“Source”:NumberInt(“21″),”Batch”: -o /data/mongodb_back/sms.csv /mongodb_back/ data/mongodb_back/sms.csv

But execution error:

XXX is not valid JSON: json: cannot unmarshal string into Go value of type json.NumberInt

The error screenshot is as follows:

Error inference and testing: the error message contains the NumberInt keyword, and the query condition also contains the keyword.

The result is to replace NumberInt(“21”) in the export command with 21 and run again.

Run the following command:

/ data/mongo/mongobin344 / bin/mongoexport – h 172 X.X.X XX – port port – db database – u account – p – authenticationDatabase authentication database ‘password’ –type= CSV -c qqwj_order -f MsgContent,REC_CreateTime -q ‘{“Source”:21,”Batch”: -o /data/mongodb_back/sms.csv /mongodb_back/ data/mongodb_back/sms.csv

The execution result is

The result shows that the modified data is exported successfully.

Error parsing and Principles explore why an export command written in the shell as “Source” : NumberInt(“21”) returns an error when viewed from a query as “Source” : NumberInt(“21”) Make sure you convert to “Source”:21

Source = Source;

“Source” : NumberInt(“21”); “Source” : NumberInt(“21”)

Look back and see how it works. We know that MongoDB currently supports four data types.

Double 32-bit INTEGER 64-bit Integer Decimal (New in Version 3.4.) Queries can be executed in MongoDB clients, but exports cannot be executed in shell. Is it going to depend on whether you insert a NumberInt or a NumberInt?

Let’s test the hypothesis.

Run NoSQLBooster for MongoDB to insert test data

Insert test data in shell mode

Use $type to see the inserted data type

Db.numbers. Find ({n:{$type:1}}) // Double; Query data whose Type is Double

The above query results show that, whether through the client or shell, when the number does not specify the data type, the default is Double.

Db.numbers. Find ({n:{$type:16}}) // Type is 32-bit integer; Query data whose Type is 32-bit INTEGER

The above query table names, either through the client or the shell, are converted to a unified 32-bit INTEGER.

3. Run the db.numbers. Find ({n:{$type:18}}) // Type is 64-bit INTEGER to query the data whose type is 64-bit integer

The above query table names, either through the client or the shell, are converted to a unified 64-bit INTEGER type.

As shown in the above tests, the data is automatically dumped when stored (regardless of the client tool, shell or NoSQLBooster for MongoDB, NumberLong(5) or NumberLong(‘5’); NumberInt(5) or NumberInt(‘5 ‘)).

Are you confused? In this case, why is the query error?

XXX is not valid JSON: JSON: cannot unmarshal String into Go value of type json.numberInt

This means that the shell thinks we passed a character type of data to json.numberInt.

So if I change NumberInt(“21”) from the exported command to NumberInt(21)

Run the following command:

/ data/mongo/mongobin344 / bin/mongoexport – h 172 X.X.X XX – port port – db database – u account – p – authenticationDatabase authentication database ‘password’ –type=csv -c qqwj_order -f MsgContent,REC_CreateTime -q ‘{“Source”: -o /data/mongodb_back/sms.csv /mongodb_back/ data/mongodb_back/sms.csv

The execution was successful.

Conclusion says a lot of summary:

The export command fails to be executed:

/ data/mongo/mongobin344 / bin/mongoexport – h 172 X.X.X XX – port port – db database – u account – p – authenticationDatabase authentication database ‘password’ –type= CSV -c qqwj_order -f MsgContent,REC_CreateTime -q ‘{“Source”:NumberInt(“21″),”Batch”: -o /data/mongodb_back/sms.csv /mongodb_back/ data/mongodb_back/sms.csv

The export command is as follows:

/ data/mongo/mongobin344 / bin/mongoexport – h 172 X.X.X XX – port port – db database – u account – p – authenticationDatabase authentication database ‘password’ –type= CSV -c qqwj_order -f MsgContent,REC_CreateTime -q ‘{“Source”:21,”Batch”: -o /data/mongodb_back/sms.csv and mongodb_back/ data/mongodb_back/sms.csv

/ data/mongo/mongobin344 / bin/mongoexport – h 172 X.X.X XX – port port – db database – u account – p – authenticationDatabase authentication database ‘password’ –type=csv -c qqwj_order -f MsgContent,REC_CreateTime -q ‘{“Source”: -o /data/mongodb_back/sms.csv /mongodb_back/ data/mongodb_back/sms.csv

The differences between the three export commands are highlighted in red.

P.s1: Later, the author made a deep study of why the same query, the general sample query results, some show “N” : 5; Some display “n” : NumberInt(“5”). Hee hee 》》》》 version is different.

Old version (partial) display

The new version (for example, nosqlBooster4Mongo-4.7.1) is displayed

P.s2: When storing numeric data, what data type will be stored actually depends on the language driving. For example, in Ruby and Python, when serializing an integer, the driver automatically determines whether to encode a 32-bit integer or a 64-bit integer. The shell needs to be specified.