Learn R Programming

nodbi (version 0.9.0)

docdb_query: Get documents with a filtering query

Description

Get documents with a filtering query

Usage

docdb_query(src, key, query, ...)

Value

Data frame with requested data, may have nested lists in columns

Arguments

src

Source object, result of call to any of functions src_mongo(), src_sqlite(), src_elastic(), src_couchdb() or src_postgres()

key

(character) A key as name of the container (corresponds to parameter collection for MongoDB, dbname for CouchDB, index for Elasticsearch and to a table name for SQLite and for PostgreSQL)

query

(character) A JSON query string, see examples. Can use multiple comparisons / tests (e.g., '$gt', '$ne', '$in', '$regex'), with at most one logic operator ('$and' if not specified, or '$or').

...

Optionally, fields a JSON string of fields to be returned from anywhere in the tree (dot paths notation).

Main functions used per database:

  • MongoDB: find() in mongolite::mongo()

  • SQLite: SQL query using json_tree()

  • Elasticsearch: elastic::Search()

  • CouchDB: sofa::db_query()

  • PostgreSQL: SQL query using built-in jsonb_build_object()

  • DuckDB: SQL using built-in json_extract()

Examples

Run this code
if (FALSE) {
src <- src_sqlite()
docdb_create(src, "mtcars", mtcars)
docdb_query(src, "mtcars", query = '{"mpg":21}')
docdb_query(src, "mtcars", query = '{"mpg":21, "gear": {"$lte": 4}}')
docdb_query(src, "mtcars", query = '{"mpg":21}', fields = '{"mpg":1, "cyl":1}')
docdb_query(src, "mtcars", query = '{"_id": {"$regex": "^.+0.*$"}}', fields = '{"gear": 1}')
# complex query, not supported for Elasticsearch and CouchDB backends at this time:
docdb_query(src, "mtcars", query = '{"$and": [{"mpg": {"$lte": 18}}, {"gear": {"$gt": 3}}]}')
}

Run the code above in your browser using DataLab