总结:

1.MongoDB 的BSON格式支持额外的数据类型

2 Date 对象内部存储64位字节存整数,存储使用NumberLong()这个类来存,使用NumberInt()存32位整数,128位十进制的用NumberDecimal()存储(这个函数是为了方便的存储财政数据)

db.collection.insertOne( { _id: 10, calc: NumberLong("2090845886852") } )

3 时间的三个方法

test@gzxkvm52> var myDateString=Date()
test@gzxkvm52> myDateString
Mon Jul 24 2017 11:16:12 GMT+0800 (CST)
test@gzxkvm52> var myDate = new Date();
test@gzxkvm52> var myDateInitUsingISODateWrapper = ISODate();
test@gzxkvm52> myDate
ISODate("2017-07-24T03:17:54.900Z")
test@gzxkvm52> myDateInitUsingISODateWrapper
ISODate("2017-07-24T03:18:02.774Z")

4 使用NunberDecimal需要注意的问题(需要使用双引号,否则会有精度问题)

test@gzxkvm52> aa = NumberDecimal("12.444444")
NumberDecimal("12.444444")
test@gzxkvm52> aa
NumberDecimal("12.444444")
test@gzxkvm52> bb = NumberDecimal(12.444444)
NumberDecimal("12.4444440000000")
test@gzxkvm52> bb
NumberDecimal("12.4444440000000")
test@gzxkvm52> bb = NumberDecimal(12.464444)
NumberDecimal("12.4644440000000")
test@gzxkvm52> bb
NumberDecimal("12.4644440000000")
test@gzxkvm52> cc = NumberDecimal(9999999.4999999999
...
... )
NumberDecimal("9999999.50000000")
test@gzxkvm52> cc
NumberDecimal("9999999.50000000")
test@gzxkvm52> dd = NumberDecimal("9999999.4999999999")
NumberDecimal("9999999.4999999999")
test@gzxkvm52> dd
NumberDecimal("9999999.4999999999")

5 使用instanceof判断数据类型(真假)

MongoDB BSON provides support for additional data types than JSONDrivers provide native support for these data types in host languages and the mongo shell also provides several helper classes to support the use of these data types in the mongo JavaScript shell. See the Extended JSON reference for additional information.

Types

Date

The mongo shell provides various methods to return the date, either as a string or as a Date object:

  • Date() method which returns the current date as a string.
  • new Date() constructor which returns a Date object using the ISODate() wrapper.
  • ISODate() constructor which returns a Date object using the ISODate() wrapper.

Internally, Date objects are stored as a 64 bit integer representing the number of milliseconds since the Unix epoch (Jan 1, 1970), which results in a representable date range of about 290 millions years into the past and future.

Return Date as a String

To return the date as a string, use the Date() method, as in the following example:

var myDateString = Date();

To print the value of the variable, type the variable name in the shell, as in the following:

myDateString

The result is the value of myDateString:

Wed Dec 19 2012 01:03:25 GMT-0500 (EST)

To verify the type, use the typeof operator, as in the following:

typeof myDateString

The operation returns string.

Return Date

The mongo shell wraps objects of Date type with the ISODate helper; however, the objects remain of typeDate.

The following example uses both the new Date() constructor and the ISODate() constructor to returnDate objects.

var myDate = new Date();
var myDateInitUsingISODateWrapper = ISODate();

You can use the new operator with the ISODate() constructor as well.

To print the value of the variable, type the variable name in the shell, as in the following:

myDate

The result is the Date value of myDate wrapped in the ISODate() helper:

ISODate("2012-12-19T06:01:17.171Z")

To verify the type, use the instanceof operator, as in the following:

myDate instanceof Date
myDateInitUsingISODateWrapper instanceof Date

The operation returns true for both.

ObjectId

The mongo shell provides the ObjectId() wrapper class around the ObjectId data type. To generate a new ObjectId, use the following operation in the mongo shell:

new ObjectId

SEE

ObjectId

NumberLong

The mongo shell treats all numbers as floating-point values by default. The mongo shell provides theNumberLong() wrapper to handle 64-bit integers.

The NumberLong() wrapper accepts the long as a string:

NumberLong("2090845886852")

The following examples use the NumberLong() wrapper to write to the collection:

db.collection.insertOne( { _id: 10, calc: NumberLong("2090845886852") } )
db.collection.updateOne( { _id: 10 },
{ $set: { calc: NumberLong("2555555000000") } } )
db.collection.updateOne( { _id: 10 },
{ $inc: { calc: NumberLong(5) } } )

Retrieve the document to verify:

db.collection.findOne( { _id: 10 } )

In the returned document, the calc field contains a NumberLong object:

{ "_id" : 10, "calc" : NumberLong("2555555000005") }

If you use the $inc to increment the value of a field that contains a NumberLong object by a float, the data type changes to a floating point value, as in the following example:

  1. Use $inc to increment the calc field by 5, which the mongo shell treats as a float:

    db.collection.updateOne( { _id: 10 },
    { $inc: { calc: 5 } } )
  2. Retrieve the updated document:

    db.collection.findOne( { _id: 10 } )
    

    In the updated document, the calc field contains a floating point value:

    { "_id" : 10, "calc" : 2555555000010 }
    

NumberInt

The mongo shell treats all numbers as floating-point values by default. The mongo shell provides theNumberInt() constructor to explicitly specify 32-bit integers.

NumberDecimal

New in version 3.4.

The mongo shell treats all numbers as 64-bit floating-point double values by default. The mongo shell provides the NumberDecimal() constructor to explicitly specify 128-bit decimal-based floating-point values capable of emulating decimal rounding with exact precision. This functionality is intended for applications that handle monetary data, such as financial, tax, and scientific computations.

The decimal BSON type uses the IEEE 754 decimal128 floating-point numbering format which supports 34 decimal digits (i.e. significant digits) and an exponent range of −6143 to +6144.

The NumberDecimal() constructor accepts the decimal value as a string:

NumberDecimal("1000.55")

The value is stored in the database as follows:

NumberDecimal("1000.55")

The NumberDecimal() constructor also accepts double values from the mongo shell (i.e. without quotes), although this is not recommended due to the risk of losing precision. The constructor creates a binary-baseddouble precision representation of the decimal-based parameter (potentially losing precision), then converts that value to a decimal value with a precision of 15 digits. The following example passes the value implicitly as a double and shows how it is created with a precision of 15 digits:

NumberDecimal(1000.55)

The value is stored in the database as follows:

NumberDecimal("1000.55000000000")

The following example passes the value implicitly as a double and shows how a loss of precision can occur:

NumberDecimal(9999999.4999999999)

The value is stored in the database as follows:

NumberDecimal("9999999.50000000")

NOTE

To use the decimal data type with a MongoDB driver, be sure to use a driver version that supports it.

Equality and Sort Order

Values of the decimal type are compared and sorted with other numeric types based on their actual numeric value. Numeric values of the binary-based double type generally have approximate representations of decimal-based values and may not be exactly equal to their decimal representations, so use theNumberDecimal() constructor when checking the equality of decimal values. Consider the following examples with the following documents in the numbers collection:

{ "_id" : 1, "val" : NumberDecimal( "9.99" ), "description" : "Decimal" }
{ "_id" : 2, "val" : 9.99, "description" : "Double" }
{ "_id" : 3, "val" : 10, "description" : "Double" }
{ "_id" : 4, "val" : NumberLong(10), "description" : "Long" }
{ "_id" : 5, "val" : NumberDecimal( "10.0" ), "description" : "Decimal" }

When the queries from the table below are plugged into the db.numbers.find(<query>) method, the following results are returned:

Query Results
{ “val”: 9.99 } { “_id”: 2, “val”: 9.99, “description”: “Double” }
{ “val”: NumberDecimal( “9.99” ) } { “_id”: 1, “val”: NumberDecimal( “9.99” ), “description”: “Decimal” }
{ val: 10 }
{ “_id”: 3, “val”: 10, “description”: “Double” }
{ “_id”: 4, “val”: NumberLong(10), “description”: “Long” }
{ “_id”: 5, “val”: NumberDecimal( “10.0” ), “description”: “Decimal” }
{ val: NumberDecimal( “10” ) }
{ “_id”: 3, “val”: 10, “description”: “Double” }
{ “_id”: 4, “val”: NumberLong(10), “description”: “Long” }
{ “_id”: 5, “val”: NumberDecimal( “10.0” ), “description”: “Decimal” }

The first query, { "val": 9.99 }, implicitly searches for the double representation of 9.99 which is not equal to the decimal representation of the value.

The NumberDecimal() constructor is used to query for the document with the decimal representation of9.99. Values of the double type are excluded because they do not match the exact value of the decimalrepresentation of 9.99.

Matching values of all numeric types are returned when querying for whole numbers. For example, querying for a double representation of 10 will include a decimal representation of 10.0 in the results and vice versa.

Checking for decimal Type

To test for decimal type, use the $type operator with the string alias "decimal" or 19, the numeric code for the decimal type.

db.inventory.find( { price: { $type: "decimal" } } )

Check Types in the mongo Shell

To determine the type of fields, the mongo shell provides the instanceof and typeof operators.

instanceof

instanceof returns a boolean to test if a value is an instance of some type.

For example, the following operation tests whether the _id field is an instance of type ObjectId:

mydoc._id instanceof ObjectId

The operation returns true.

typeof

typeof returns the type of a field.

For example, the following operation returns the type of the _id field:

typeof mydoc._id

In this case typeof will return the more generic object type rather than ObjectId type.

4.Data Types in the mongo Shell-官方文档摘录的更多相关文章

  1. 2.Access the mongo Shell Help-官方文档摘录

    总结: 1.使用help可以查看帮助信息db.help()  help等 2.查看对应的实现方法.比如 test@gzxkvm52$ db.updateUser function (name, upd ...

  2. Cocos Creator 加载和切换场景(官方文档摘录)

    Cocos Creator 加载和切换场景(官方文档摘录) 在 Cocos Creator 中,我们使用场景文件名( 可以不包含扩展名)来索引指代场景.并通过以下接口进行加载和切换操作: cc.dir ...

  3. MongoDB - The mongo Shell, Data Types in the mongo Shell

    MongoDB BSON provides support for additional data types than JSON. Drivers provide native support fo ...

  4. ng的概念层次(官方文档摘录)

    官方文档是这么说的: You write Angular applications by: composing HTML templates with Angularized markup, writ ...

  5. Cocos Creator 生命周期回调(官方文档摘录)

    Cocos Creator 为组件脚本提供了生命周期的回调函数.用户通过定义特定的函数回调在特定的时期编写相关 脚本.目前提供给用户的声明周期回调函数有: onLoad start update la ...

  6. Cocos Creator 使用计时器(官方文档摘录)

    在 Cocos Creator 中,我们为组件提供了方便的计时器,这个计时器源自于 Cocos2d-x 中的 cc.Scheduler,我们将它保留在了 Cocos Creator 中并适配了基于组件 ...

  7. angular 模板语法(官方文档摘录)

    https://angular.cn/guide/template-syntax {{}} 和"" 如果嵌套,{{}}里面求完值,""就是原意 <h3&g ...

  8. Spring Data Commons 官方文档学习

    Spring Data Commons 官方文档学习   -by LarryZeal Version 1.12.6.Release, 2017-07-27 为知笔记版本在这里,带格式. Table o ...

  9. Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion(一)

    题外话:本篇是对之前那篇的重排版.并拆分成两篇,免得没了看的兴趣. 前言 在Spring Framework官方文档中,这三者是放到一起讲的,但没有解释为什么放到一起.大概是默认了读者都是有相关经验的 ...

随机推荐

  1. C# 正则表达式替换制定关键词后面的所有内容

    如题,将 {OUTSCIPTE} 关键词后的所有内容替换为string.Empty(包含关键字)这个正则该怎么写?我是 {OUTSCIPTE}(.*)$ 写的但是什么反应也没有 string str= ...

  2. Java创建多线程的三种方法

    Java多线程实现方式主要有三种:继承Thread类.实现Runnable接口.使用ExecutorService.Callable.Future实现有返回结果的多线程.其中前两种方式线程执行完后都没 ...

  3. 关于http ,那些你必须知道的事

    一,HTTP简介 1,HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览 ...

  4. 蓝桥杯第五届B组 李白打酒

    外面的小雨下着,风吹着,在实验室敲着代码 时隔一年之后再次做这道题,依然神一样的回溯出来: 标题:李白打酒 话说大诗人李白,一生好饮.幸好他从不开车. 一天,他提着酒壶,从家里出来,酒壶中有酒2斗.他 ...

  5. 您的位置:首页 » IOS » iOS中全局悬浮按钮,类似IPhone中的AssistiveTouch iOS中全局悬浮按钮,类似IPhone中的AssistiveTouch

    原文地址:http://blog.5ibc.net/p/86562.html 前提:当时看到别人写过这个类似AssistiveTouch的demo,但是有问题,第一改变不了位置.第二切换页面后无法使用 ...

  6. 跟着百度学PHP[17]-PHP扩展CURL的用法详解

    实现的功能: 1.实现远程获取和采集内容2.实现PHP 网页版的FTP上传下载3.实现模拟登陆:去一个邮件系统,curl可以模拟cookies4.实现接口对接(API),数据传输等:通过一个平台发送短 ...

  7. C# 时间格式 yyyy/mm/dd

    今天遇到个问题在C#中将日期格式设置为yyyy/MM/dd,我是这样写的: DateTime.Now.ToString("yyyy/MM/dd"); 可是获取到的日期还是显示yyy ...

  8. usb 安装系统

    写在前面 本文是先安装windows再安装linux,并通过windows引导linux的启动项.这样方便linux的反复重装.折腾等. 光盘安装和U盘安装基本差不多,只是U盘安装多了把镜像文件写到U ...

  9. js移除某一类的div

    (转载)Javascript removeChild()不能删除全部子节点的解决办法   在Javascript中,只提供了一种删除节点的方法:removeChild().removeChild() ...

  10. mac上制作u盘启动盘

    Mac上制作Ubuntu USB启动盘 一.下载ubuntu iso镜像 二.将iso转换为img文件 $ hdiutil convert -format UDRW -o /path/to/gener ...