Data types for MongoDB

Source: Internet
Author: User
Tags lua mongodb find mongodb version mongo shell

Writing a LUA module recently MongoDB version 3.2,lua is 5.3.1. The underlying is written in C + +, and the function is exposed to the LUA call. But when you print the results in Lua, something strange happens. First, the contents of the database:

> Db.item.Find(){ "_id":2001,"Amount":999 }{ "_id":2002,"Amount": +,"name":"Kfsjadlfasfkljeihfdsfkasfjslkfjei"}

Of course, this is what I write to test, it doesn't make sense. However, printing in Lua is like this:

Table0xff0ae0{    "1"= table:0xfe4800    {        "name"="Kfsjadlfasfkljeihfdsfkasfjslkfjei"        "_id"=2002.0        "Amount"=78.0    }    "0"= table:0xfe4610    {        "Amount"=999.0        "_id"=2001.0    }}

The data is correct, the problem is: Like 2001 of these shaping numbers for everything has a decimal point. With a little trace of the code, you can see that the data from MongoDB find is already double, so lua_pushnumber instead of Lua_pushinteger when you return to the LUA layer. Causes Lua to think of the number as not an integer. This is how the decimal point comes in.

However, MongoDB data is based on Bson, while Bson is of type int. Then, the data is entered in the MongoDB type is already double. And the data I entered in the MONGO shell, the problem has to be traced from the MONGO shell.

As mentioned in the official website https://docs.mongodb.org/v3.0/core/shell-types/, the MONGO shell is of a type. However, when we enter it, we usually enter it in this way:

Db.item.insert ({_id:2002, Amount:)

No type is specified, and the JSON format is used. The problem is: The JSON format has only number type, and does not subdivide numbers such as int, double, so for the sake of insurance, it is obvious that the MONGO shell all save it as a double type. It was clear that the authorities had long noticed the problem, allowing the type to be specified when entering data in the shell, such as:

Db.item.insert ({_id:numberint), amount: +,name:"  Kfsjadlfasfkljeihfdsfkasfjslkfjei"})

Use a function such as numberint to specify the type. Once specified, this is the current query in Lua, with the result:

Table0xff0ae0{    "2"= table:0xfe5cd0    {        "name"="Kfsjadlfasfkljeihfdsfkasfjslkfjei"        "_id"= the        "Amount"=78.0    }    "1"= table:0xfe4800    {        "name"="Kfsjadlfasfkljeihfdsfkasfjslkfjei"        "_id"=2002.0        "Amount"=78.0    }    "0"= table:0xfe4610    {        "Amount"=999.0        "_id"=2001.0    }}

Discovery Data 2009 is displayed as normal. Later, when the program is running, the data is entered from the program, which is not a problem.

Data types for MongoDB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.