Skip to content
ahuang007 edited this page Aug 21, 2020 · 4 revisions

skynet 集成了 mongo 的 driver 详见 mongo.lua 支持以下接口:

  • mongo_collection:insert,

  • mongo_collection:update,

  • mongo_collection:delete,

  • mongo_collection:find,

  • mongo_collection:findAndModify,

  • mongo_collection:createIndex,

  • mongo_collection:dropIndex,

  • mongo_cursor:next,

  • mongo_cursor:sort,

  • mongo_cursor:skip,

  • mongo_cursor:limit,

  • mongo_cursor:count,

  • mongo_client:runCommand ...

以上接口会返回 `bson.decode` 的结果,
对于返回需要获得某字段的类型时,使用 `bson.type` 会更严谨。`bson.type` 会返回两个值,第一个是数据的类型,第二个是数据本身,
特别的,为了区分 bson 的 null 与 lua 层的 nil,`bson.type` 在检查到数据类型为 TNIL 时,数据类型返回为"nil",数据本身返回 nil,
当检查到数据类型为 bson 的 null 时,数据类型返回为"nil",数据本身返回一个特殊的字符串。
若只是想比较 bson 的某个字段是否为 bson 的 null,则可以直接与 `bson.null` 进行比较
(如在检查 `findAndModify` 的结果时,检查是否找到并修改了数据,可以检查返回值的 value 字段与 `bson.null` 是否相等)。
mongo.lua 里面已经封装了bson.decode 和 bson.encode 
Clone this wiki locally