search
HomeDatabaseMysql Tutorialmongodb入门-3数据类型--基本数据类型

mongodb入门-3数据类型--基本数据类型 MongoDB的文档使用BSON(Binary JSON)来组织数据,BSON类似于JSON,JSON只是一种简单的表示数据的方式,只包含了6种数据类型(null、布尔、数字、字符串、数组及对象),不能完全满足复杂业务的需要,因此,BSON还提供

mongodb入门-3数据类型--基本数据类型

 

MongoDB的文档使用BSON(Binary JSON)来组织数据,BSON类似于JSON,JSON只是一种简单的表示数据的方式,只包含了6种数据类型(null、布尔、数字、字符串、数组及对象),不能完全满足复杂业务的需要,因此,BSON还提供日期、32位数字、64位数字等类型。以下对mongoDB的类型进行简要说明:

1、 null  null类型用于表示空值或不存在的字段,如:{“one”:null}

2、 布尔类型 布尔类型有两上值,’true’和’false’ ,如:{“one”:true}

3、 32位整数    在由于mongoDB的控制台使用JS引擎进行输入,而JS仅支持64位浮点数,所以32位整数将会被自动转义;

4、 64位整数

64位整数与32位整数一样,在MongoDB控制台使用时,会转义成64位浮点数。除外,如果数据库本身存储的数据类型无论是32位整数还是64位整数,使用MongoDB控制台获取后,更改其文档记录(即使没有修改整数本身,只修改了文档的其他部分),并重新使用控制台写回数据库,则其数据类型也会变成了64位浮点数。

除外,使用控制台查看一个64位整数时,可能会不正确定,原因是有些64位的整数不能精确表示为64位浮点数,而控制台呈示都是64位浮点数。

5、 64位浮点数   MongoDB控制台数字的默认类型,如:{“one”:2.02} {“one”:10}

6、 字符串 如:{“one”:”Hello World”}

7、 符号 在MongoDB控制台中不支持这种类型,将自动转义成字符串;

8、 对象id 对象id是文档中唯一的12位的ID ,

在MongoDB来存储文档时,必须有一个“_id”键,这个键可以是任何类型,如果在增加文档时,没有这个_id键,则系统会使用ObjectId对象自动生成一个,在分布式环境中,不同的机器都能用全局唯一的同种方法来生成值,其生成规则为:

0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11

     时间戳    |     机器    |   PID |    计数器

前4位表示时间戳,时间戳以秒为单位,由于时间戳在前面,可以更好地反映出数据插入时的时间顺序,使的数据更容易查询,建议索引更加容易。

虽然系统会自动创建_id键,但在高并发的应用下建议使用客户端的驱动程序来创建,主要原因是,尽管ObjectId可以生成,但是系统在生成时,还是会产生开销,增加数据库的负担。

在高并发的分布式环境中,只使用以秒为单位的时间戳和机器不能区分其唯一性,故在其后面添加了PID,即MongoDB的进程标识符,前9个字符保证了同一秒钟不同机器不同进程产生的ObjectId是唯一,两位是一个自动递增的计数器,确保相同进程同一秒产生的ObjectId也不一样。

9 日期 从标准纪元开始的毫秒数 { “date”:new Date()}

10 正则表达式 文档中可以包含正则表达式,遵循 JavaScript 的语法 { “foo”:/foobar/i}

11 代码 文档中可以包含 JavaScript 代码 { “x”: function() {}}

12二进制数据 任意字节的二进制串组成, shell 不支持 

 

13 最大值 表示可能的最大值, shell 不支持

14 最小值 表示可能的最小值, shell 不支持

15 未定义 undefined { “x”: undefined}

16 数组 值的集合或者列表 { “arr”: [“a”,“b”]}

17内嵌文档 文档可以作为文档中某个 key 的value { “x”:{“foo”:“bar”}}

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does MySQL handle concurrency compared to other RDBMS?How does MySQL handle concurrency compared to other RDBMS?Apr 29, 2025 am 12:44 AM

MySQLhandlesconcurrencyusingamixofrow-levelandtable-levellocking,primarilythroughInnoDB'srow-levellocking.ComparedtootherRDBMS,MySQL'sapproachisefficientformanyusecasesbutmayfacechallengeswithdeadlocksandlacksadvancedfeatureslikePostgreSQL'sSerializa

How does MySQL handle transactions compared to other relational databases?How does MySQL handle transactions compared to other relational databases?Apr 29, 2025 am 12:37 AM

MySQLhandlestransactionseffectivelyusingtheInnoDBengine,supportingACIDpropertiessimilartoPostgreSQLandOracle.1)MySQLusesREPEATABLEREADasthedefaultisolationlevel,whichcanbeadjustedtoREADCOMMITTEDforhigh-trafficscenarios.2)Itoptimizesperformancewithabu

What are the data types available in MySQL?What are the data types available in MySQL?Apr 29, 2025 am 12:28 AM

MySQL data types are divided into numerical, date and time, string, binary and spatial types. Selecting the correct type can optimize database performance and data storage.

What are some best practices for writing efficient SQL queries in MySQL?What are some best practices for writing efficient SQL queries in MySQL?Apr 29, 2025 am 12:24 AM

Best practices include: 1) Understanding the data structure and MySQL processing methods, 2) Appropriate indexing, 3) Avoid SELECT*, 4) Using appropriate JOIN types, 5) Use subqueries with caution, 6) Analyzing queries with EXPLAIN, 7) Consider the impact of queries on server resources, 8) Maintain the database regularly. These practices can make MySQL queries not only fast, but also maintainability, scalability and resource efficiency.

How does MySQL differ from PostgreSQL?How does MySQL differ from PostgreSQL?Apr 29, 2025 am 12:23 AM

MySQLisbetterforspeedandsimplicity,suitableforwebapplications;PostgreSQLexcelsincomplexdatascenarioswithrobustfeatures.MySQLisidealforquickprojectsandread-heavytasks,whilePostgreSQLispreferredforapplicationsrequiringstrictdataintegrityandadvancedSQLf

How does MySQL handle data replication?How does MySQL handle data replication?Apr 28, 2025 am 12:25 AM

MySQL processes data replication through three modes: asynchronous, semi-synchronous and group replication. 1) Asynchronous replication performance is high but data may be lost. 2) Semi-synchronous replication improves data security but increases latency. 3) Group replication supports multi-master replication and failover, suitable for high availability requirements.

How can you use the EXPLAIN statement to analyze query performance?How can you use the EXPLAIN statement to analyze query performance?Apr 28, 2025 am 12:24 AM

The EXPLAIN statement can be used to analyze and improve SQL query performance. 1. Execute the EXPLAIN statement to view the query plan. 2. Analyze the output results, pay attention to access type, index usage and JOIN order. 3. Create or adjust indexes based on the analysis results, optimize JOIN operations, and avoid full table scanning to improve query efficiency.

How do you back up and restore a MySQL database?How do you back up and restore a MySQL database?Apr 28, 2025 am 12:23 AM

Using mysqldump for logical backup and MySQLEnterpriseBackup for hot backup are effective ways to back up MySQL databases. 1. Use mysqldump to back up the database: mysqldump-uroot-pmydatabase>mydatabase_backup.sql. 2. Use MySQLEnterpriseBackup for hot backup: mysqlbackup--user=root-password=password--backup-dir=/path/to/backupbackup. When recovering, use the corresponding life

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.