您的位置:首页 > 数据库 > Mongodb

Mongodb 之 数据类型

2012-08-14 09:46 330 查看
数据类型

存储并检索非JSON类型Mongodb原始类型(ObjectID,Long,Binary,Timestamp,DBRef,Code)。

特别的是,每个文档(行)都有一个可以为任意数据类型的唯一_id,默认时为12字节的对象标示(ObjectID)

ObjectIDs能表示为24位十六进制的字符串。但是在数据库中使用之前,要将其转为一个ObjecID。例子如下:

// 得到一个objectID类型

var ObjectID = require('mongodb').ObjectID;

var idString = '4e4e1638c85e808431000003';
collection.findOne({_id: new ObjectID(idString)}, console.log)  // new ObjectID(idString)就 ok
collection.findOne({_id: idString}, console.log)  //直接使用就 wrong! callback gets undefined

下面是非javascript的二进制形式的JSON(BSON-----Binary JSON)的原始数据类型的构造函数:

// Fetch the library
var mongo = require('mongodb');
// Create new instances of BSON types
new mongo.Long(numberString)
new mongo.ObjectID(hexString)
new mongo.Timestamp()  // the actual unique number is generated on insert.
new mongo.DBRef(collectionName, id, dbName)
new mongo.Binary(buffer)  // takes a string or Buffer
new mongo.Code(code, [context])
new mongo.Symbol(string)
new mongo.MinKey()
new mongo.MaxKey()
new mongo.Double(number)    // Force double storage


DBRefs 有以下字段(即构成函数的三个参数):

$ref 指文档(行)所在集合的名称$id 包括关联文档(行)中的_id字段的值$db 可选项 包含关联文档(行)的数据库的名称
因此DBRef文档和下面的类似:

{ $ref : <value>, $id : <value>, $db : <value> }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: