03 mongodb官方文档翻译 Data Types in the mongo Shell¶

Types

  • Date
    多种方式来获取时间

    1. Date() 方法来返回当前时间的字符串格式
    2. new Date() ISODate() 通过构造函数返回一个Date对象
  • ObjectId
    用来生成一个新的ObjectId, 使用方式
    new ObjectId()

  • NumberLong
    默认情况下所有的数字都作为了浮点型, 可以使用NumberLong来把数字转换成 64bit的 integer . eg: NumberLong("2090845886852")
    事例:

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) } } )
  • NumberInt
    与NumberLong类似,表示数字是 32bit的 Integer

  • NumberDecimal
    默认情况下数字的类型是64bit的 浮点类型,NumberDecimal 表示数字是128bit的浮点类型 ,接送的参数可以是字符串,也可以是一个浮点型
    eg: NumberDecimal("1000.55") NumberDecimal(1000.55)

  • 通过类型查询
    db.inventory.find( { price: { $type: "decimal" } } ) 查询数据类型是decimal的数据

  • 检查数据的类型

    1. instanceof
      mydoc._id instanceof ObjectId 返回的结果是 true
    2. typeof
      typeof mydoc._id

你可能感兴趣的:(03 mongodb官方文档翻译 Data Types in the mongo Shell¶)