mongo 学习三--mongodb 数据类型

3 篇文章 0 订阅
3 篇文章 0 订阅

e文:http://docs.mongodb.org/manual/core/shell-types/

Data Types in the mongo Shell

MongoDB BSON provide support for additional data types thanJSON. Drivers provide nativesupport for these data types in host languages and themongo shell also provides several helper classes to supportthe use of these data types in the mongo JavaScriptshell. See MongoDB Extended JSON for additionalinformation.

Date

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

  • Date() method which returns the current date as a string.
  • Date() constructor which returns an ISODate object when usedwith the new operator.
  • ISODate() constructor which returns an ISODate object whenused with or without the new operator.

Consider the following examples:

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

    var myDateString = Date();
    
    • To print the value of the variable, type the variable name in theshell, 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 thefollowing:

      typeof myDateString
      

      The operation returns string.

  • To get the date as an ISODate object, instantiate a new instanceusing the Date() constructor with the new operator, as in thefollowing example:

    var myDateObject = new Date();
    
    • To print the value of the variable, type the variable name in theshell, as in the following:

      myDateObject
      

      The result is the value of myDateObject:

      ISODate("2012-12-19T06:01:17.171Z")
      
    • To verify the type, use the typeof operator, as in thefollowing:

      typeof myDateObject
      

      The operation returns object.

  • To get the date as an ISODate object, instantiate a new instanceusing the ISODate() constructor without the new operator,as in the following example:

    var myDateObject2 = ISODate();
    

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

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

      myDateObject2
      

      The result is the value of myDateObject2:

      ISODate("2012-12-19T06:15:33.035Z")
      
    • To verify the type, use the typeof operator, as in thefollowing:

      typeof myDateObject2
      

      The operation returns object.

ObjectId

The mongo shell provides the ObjectId() wrapper classaround ObjectId data types. To generate a new ObjectId, usethe following operation in the mongo shell:

new ObjectId

See

ObjectId for full documentation of ObjectIds inMongoDB.

NumberLong

By default, the mongo shell treats all numbers asfloating-point values. The mongo shell provides theNumberLong() class to handle 64-bit integers.

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

NumberLong("2090845886852")

The following examples use the NumberLong() class to write to thecollection:

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 aNumberLong object:

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

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

  1. Use $inc to increment the calc field by 5, which themongo 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 floatingpoint value:

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

NumberInt

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值