Deleting data

Now that we have some data in our database, we can look at how we can delete specific documents. As usual, the query is very straightforward. The first thing to do is select the document to be deleted. This can be done using the document's primary key or using the filter() command. The next step is chaining the delete() command at the end of the query.

As an example, let's delete Ted's document from the people table:

r.table('people').filter({"name": "Ted"}).delete()

First, we select the correct table, then we use the filter() command to specify the predicate. In this case, we filter documents that have the name attribute equal to Ted. Finally, we call the delete() command. The resulting JSON indicates that one document has indeed been removed:

{
"deleted": 1 ,
"errors": 0 ,
"inserted": 0 ,
"replaced": 0 ,
"skipped": 0 ,
"unchanged": 0
}

Removing all documents

Occasionally, we may want to clear the entire table by removing all the documents contained in it. As in the previous section, we still need to call the delete() command without specifying any filter. The absence of the filter() command will match all the documents; therefore, chaining the delete() command at the end of the query will clear all the data:

r.table('people').delete()

Note

Note that this query does not delete the table itself; it just removes all data from the specified table.

We might want to check if the documents have indeed been removed; we can do this by counting the number of documents contained in the table:

r.table('people').count()

The count() command returns the number of documents in the resulting JSON. In this case, the result is 0, thus confirming that our previous delete() query succeeded:

Removing all documents

Deleting a table

After having deleted all the documents contained within a table, we may want to delete the table itself. This can be achieved with the tableDrop() command that accepts the table name as input.

Suppose we wanted to delete the people table from the test database, we would run the following query:

r.db("test").tableDrop("people")

RethinkDB responds to this query by acknowledging the table deletion:

{
"config_changes": [
  {
    "new_val": null ,
    "old_val": {
    "db": "test" ,
    "durability": "hard" ,
    "id": "a29447ca-1587-4b8c-86db-a7b3d5198519" ,
    "name": "people" ,
    "primary_key": "id" ,
    "shards": [
    {
    "primary_replica": "rethinkdb1" ,
    "replicas": [
    "rethinkdb1"
    ]
    }
    ] ,
    "write_acks": "majority"
    }
  }
  ] ,
  "tables_dropped": 1
}

As you can see from the previous output, RethinkDB provides us with a config_changes array where the old value is the table called people, and the new value is null, meaning that the table has been successfully deleted from the database.

Note

Please note that removing a table will delete all the data that is contained within the table—use this command with caution!

Deleting a database

The previous step required when cleaning up the database is to remove the database itself. This can be done using the dbDrop() command. Deleting a database is a permanent operation, so you may want to list all databases on the system just to make sure that you're removing the correct database.

You can get a list of all database names by running the following query:

r.dbList()

This will result in the following JSON array:

[
"rethinkdb" ,
"test"
]

Now that we're sure of wanting to permanently remove the test database, we can do so like this:

r.dbDrop("test")

This query will delete the entire test database including all tables, documents, and metadata.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.166.75