博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MongoDB - The mongo Shell, Data Types in the mongo Shell
阅读量:5098 次
发布时间:2019-06-13

本文共 4221 字,大约阅读时间需要 14 分钟。

MongoDB  provides support for additional data types than .  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  JavaScript shell. See the  reference for additional information.

 

Types

Date

The  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,  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:

Thu Dec 01 2016 00:44:26 GMT+0800 (CST)

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 type Date.

The following example uses both the new Date() constructor and the ISODate() constructor to return Date 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("2016-11-30T16:48:47.527Z")

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

myDate instanceof DatemyDateInitUsingISODateWrapper instanceof Date

The operation returns true for both.

ObjectId

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

new ObjectId

SEE: 

NumberLong

By default, the  shell treats all numbers as floating-point values. The  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.insert( { _id: 10, calc: NumberLong("2090845886852") } )db.collection.update( { _id: 10 }, { $set:  { calc: NumberLong("2555555000000") } } )db.collection.update( { _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  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  to increment the calc field by 5, which the  shell treats as a float:
    db.collection.update( { _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

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

 

Check Types in the mongo Shell

To determine the type of fields, the  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.

 

转载于:https://www.cnblogs.com/huey/p/6120385.html

你可能感兴趣的文章
Html5 离线页面缓存
查看>>
[php]在PHP中读取和写入WORD文档的代码
查看>>
WCF傻瓜模式写程序
查看>>
《绿色·精简·性感·迷你版》易语言,小到不可想象
查看>>
Java Web学习总结(13)Listener监听器
查看>>
开始Flask项目
查看>>
Ruby:多线程队列(Queue)下载博客文章到本地
查看>>
Android打包key密码丢失找回
查看>>
03 jQuery动画
查看>>
医药箱APP静态小项目
查看>>
安装使用eclipse
查看>>
VC6.0调试技巧(一)(转)
查看>>
用Chrome调试Android手机上的网页
查看>>
django 王中王8之踏青撒花
查看>>
学习网站收集
查看>>
linux命令
查看>>
类库与框架,强类型与弱类型的闲聊
查看>>
webView添加头视图
查看>>
字符环(openjudge 2755)
查看>>
php match_model的简单使用
查看>>