在 RethinkDB 中,检查数据库或表是否存在的最简单方法是什么? [英] In RethinkDB, what is the easiest way to check if a database or a table exists?

查看:47
本文介绍了在 RethinkDB 中,检查数据库或表是否存在的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我能做到的一种方法是通过dbList()tableList() 列出,然后在结果中查找我想要的内容.

One way I know I can do it is by listing throughdbList() and tableList() and then looking for what I want in the results.

有没有更简单的方法?

编辑

我的目标是创建一个表,以防它不存在.

My goal is to create a table in case it doesn't exist.

推荐答案

如果你想在数据库不存在的情况下创建它,或者如果它存在就得到一个类似数据库已经存在"的值,你可以这样做以下内容:

If you want to create a database if it does not exists, or get a value like "database already exists" if it does exist, you could do something like the following:

r.dbList().contains('example_database')
  .do(function(databaseExists) {
    return r.branch(
      databaseExists,
      { dbs_created: 0 },
      r.dbCreate('example_database')
    );
  }).run();

如果它被创建将返回以下内容:

It will return the following if it is created:

{
  "config_changes": [
    {
      "new_val": {
        "id": "1ee7ddb4-6e2c-43bb-a0f5-64ef6a6211a8",
        "name": "example_database"
      },
      "old_val": null
    }
  ],
  "dbs_created": 1
}

如果它已经存在:

{
  "dbs_created": 0
}

这篇关于在 RethinkDB 中,检查数据库或表是否存在的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆