A DRDL schema is required to match SQL queries with MongoDB aggregation
language because MongoDB do not store structured data. However, your data may
have a intrinsic structure, which is computed. mtcars_schema()
provides
precomputed schema for the mtcars
dataset, see also ?mtcars
for example
and testing purpose in the absence of the "mongodrdl" binaries (correct
providing the mtcars
data are inserted unmodified in an empty collection).
Arguments
- mongo
A mongo object as obtained from
mongo()
orattr(tbl_mongo(), "mongo")
.- max_scan
The maximum of documents to scan to elaborate the structure (100 by default).
- recalc
Is the schema recalculated, in case it is present in the mongo object (
FALSE
by default)?- path
The path to the "mongodrdl" binaries. If it is accessible on the search path, or indicated in the "mongotranslate.path" option, no need to specify it.
- x
A mongo_schema object.
- ...
Further arguments passed to
print()
(not used currently).- db
The name of the database.
- collection
The name of the collection.
Value
A mongo_schema object with the schema as character string and a sample attribute that contains a data.frame with the three first documents in the collection, as an example of what is in that collection.
Details
The MongoDB BI Connector's "mongodrdl" external program is used to compute
the schema, unless a "schema" attribute is found in the mongo object and
recalc = FALSE
.
Examples
if (FALSE) {
# We use the same little MongoDB server with mtcars set up for {mongolite}
library(mongoplyr)
database <- "test"
collection <- "mtcars"
mongodb_url <- "mongodb+srv://readwrite:test@cluster0-84vdt.mongodb.net"
# Connect and make sure the collection contains the mtcars dataset
mcon <- mongolite::mongo(collection, database, mongodb_url)
mcon$drop()
mcon$insert(mtcars)
# mtcars_schema() returns a precomputed schema for the mtcars dataset
schema <- mtcars_schema(db = database, collection = collection)
schema
# This schema can be added as an attribute to the connection to avoid
# recalculating it every time
attr(mcon, 'schema') <- schema
# Calling mongo_schema() on a mongo objet that already has a schema attached
# just retrieves it (no recalculation)
schema2 <- mongo_schema(mcon)
identical(schema, schema2)
# ... unless there is nothing attached, or you use recalc = TRUE
# In this case, "mongodrdl" must be installed and accessible
# see vignette("mongoplyr")
schema3 <- mongo_schema(mcon, recalc = TRUE)
schema3
# There is no particular order in the documents collection. So, do not expect
# to get always the same three documents as example
# However, the schema in itself should be fairly consistent (extracted with
#`as.character()` here)
identical(as.character(schema), as.character(schema3))
mcon$disconnect()
}