Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the MongoDB data types

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

MongoDB data types, I believe that many inexperienced people are helpless about this, this article summarizes the causes and solutions of the problem, through this article I hope you can solve this problem.

digital

The shell defaults to 64-bit floating-point values, as follows:

db.sang_collec.insert({x:3.1415926})db.sang_collec.insert({x:3})

For integer values, we can use NumberInt or NumberLong as follows:

db.sang_collec.insert({x:NumberInt(10)})db.sang_collec.insert({x:NumberLong(12)})

string

Strings can also be stored directly, as follows:

db.sang_collec.insert({x:"hello MongoDB! "})

regular expression

Regular expressions are mainly used in queries. When querying, we can use regular expressions. The syntax is the same as that of regular expressions in JavaScript. For example, query all documents with key x and value starting with hello and are case-insensitive:

db.sang_collec.find({x:/^(hello)(. [a-zA-Z0-9])+/i})

array

Arrays are also supported, as follows:

db.sang_collec.insert({x:[1,2,3,4,new Date()]})

The data types in an array can be varied.

date

MongoDB supports Date type data. You can directly new a Date object, as follows:

db.sang_collec.insert({x:new Date()})

embedded document

A document can also be used as the value of another document, which is actually very easy to understand, as follows:

db.sang_collect.insert({name:"Romance of the Three Kingdoms",author:{name:"Luo Guanzhong",age:99}});

A book has an attribute of author, and the author has attributes such as name and age.

ObjectId

As mentioned earlier, every time we insert a piece of data, the system will automatically insert a_id key for us. The value of this key cannot be repeated. It can be of any type. We can also insert it manually. By default, its data type is ObjectId. Since MongoDB was originally designed as a distributed database, using ObjectId can avoid duplication of_id in different databases.(Duplicate_id values occur in distributed systems if autoincrement is used), a feature somewhat similar to the difference between version numbers in Git and version numbers in Svn.

ObjectId uses 12 bytes of storage space, each byte can store two hexadecimal digits, so a total of 24 hexadecimal digits can be stored in the string, in which the first 8 bits represent the timestamp, the next 6 bits are a machine code, the next 4 bits represent the process id, and the last 6 bits represent the counter.

binary

Binary data can also be stored in MongoDB, but this is not often the case, binary data storage can not be operated in the shell, we will introduce this storage method in the following code.

code

Documentation can also include JavaScript code, such as:

db.sang_collect.insert({x:function f1(a,b){return a+b;}});

After reading the above, do you know what methods MongoDB data types have? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report