Learn R Programming

nodbi (version 0.3.0)

docdb_query: Get documents with a filtering query

Description

Get documents with a filtering query

Usage

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

Arguments

src

source object, result of call to src

key

(character) A key (collection for mongo)

query

various. see Query section below.

...

Additional named parameters passed on to each package:

What is expected for each source

  • CouchDB: a list, see docs for sofa::db_query()

  • Elasticsearch: query parameters, see elastic::Search(); passed to the query parameter of elastic::Search, thus performs a URI based search where the query is passed in the URI instead of the body. In theory you can instead pass in a JSON or list to the body parameter, but if you want to do complicated Elasticsearch queries you may be better of using elastic package directly

  • MongoDB: query parameters, see mongolite docs for help with searches

  • SQLite: fields, an optional json string of fields to be returned from anywhere in the tree. Parameter query, a json string In analogy to MongoDB, a comma separated list of expressions provides an implicit AND operation. Nested or otherwise complex queries are not yet supported.

Not supported yet

  • Redis

Details

Note that with etcd, you have to prefix a key with a forward slash.

Examples

Run this code
# NOT RUN {
# CouchDB
(src <- src_couchdb())
if (docdb_exists(src, "mtcars2")) docdb_delete(src, "mtcars2")
invisible(docdb_create(src, key = "mtcars2", value = mtcars))
docdb_exists(src, "mtcars2")
(query <- list(cyl = list("$gt" = 6)))
docdb_query(src, "mtcars2", query = query)

# Elasticsearch
src <- src_elastic()
if (docdb_exists(src, "iris")) docdb_delete(src, "iris")
docdb_create(src, "iris", iris)
docdb_exists(src, "iris")
docdb_query(src, "iris", query = "setosa")
docdb_query(src, "iris", query = "1.5")
docdb_query(src, "iris", query = "Petal.Width:1.5")

# Mongo
src <- src_mongo(collection = "mtcars")
if (docdb_exists(src, "mtcars")) docdb_delete(src, "mtcars")
docdb_create(src, "mtcars", mtcars)
docdb_query(src, "mtcars", query = '{"mpg":21}')
docdb_query(src, "mtcars", query = '{"mpg":21}', fields = '{"mpg":1, "cyl":1}')
docdb_get(src, "mtcars")

# SQLite
src <- src_sqlite()
docdb_create(src, "mtcars", mtcars)
docdb_query(src, "mtcars", query = "{}", fields = '{"mpg":1, "cyl":1}')
docdb_query(src, "mtcars", query = '{"gear": {"$lte": 4}}', fields = '{"gear": 1}')
# for RSQLite from 2.1.2 using PCRE regular expressions
docdb_query(src, "mtcars", query = '{"_id": {"$regex": "^.+0.*$"}}', fields = '{"gear": 1}')
# }

Run the code above in your browser using DataLab