getOneRec {Proc4}R Documentation

Fetches Messages from a Mongo databas

Description

This function fetches P4Message objects from a mongo database. The message parser is passed as an argument, allowing it to fetch other kinds of objects than P4Messages. The function getManyRecs retrieves all matching objects and the function getOneRec retrieves the first matching object.

Usage

getOneRec(jquery, col, parser, sort = c(timestamp = -1))
getManyRecs(jquery, col, parser, sort = c(timestamp = 1), limit=0)

Arguments

jquery

A string providing a Mongo JQuery to select the appropriate records. See buildJQuery.

col

A mongo collection object to be queried.

parser

A function which will take the list of fields returned from the database and build an appropriate R object. See parseMessage.

sort

A named numeric vector giving sorting instructions. The names should correpond to fields of the objects, and the values should be positive or negative one for increasing or decreasing order. Use the value NULL to leave the results unsorted.

limit

A numeric scalar giving the maximum number of objects to retrieve. If 0, then all objects matching the query will be retrieved.

Details

This function assumes that a number of objects (usually, but not necessarily subclasses of P4Message objects) have been stored in a Mongo database. The col argument is the mongo object in which they are stored. These functions retrive the selected objects.

The first argument should be a string containing a JSON query document. Normally, thes are constructed through a call to buildJQuery.

The query is used to create an iterator over JSON documents stored in the database. At each round, the iterator extracts the JSON document as a (nested) list structure. This is pased to the parser function to build an object of the specified type. See the parseMessage function for an example parser.

The sorting argument controls the way the returned list of objects is sorted. This should be a numeric vector with names giving the field for sorting. The default values c("timestamp"=1) and c("timestamp"=-1) sort the records in ascending and decending order respectively. In particular, the default value for getOneRec means that the most recent value will be returned. The defaults assume that “timestamp” is a field of the stored object. To supress sorting of outputs, use NULL as the argument to sort.

Value

The function getOneRec returns an object whose type is determined by the output of the parser function. If parseMessage is used, this will be a P4Message object.

The function getManyRecs returns a list of object whose type is determined by the output of the parser function.

Author(s)

Russell Almond

References

The MongoDB 4.0 Manual: https://docs.mongodb.com/manual/

See Also

saveRec, parseMessage, getOneRec, getManyRecs mongo

Examples


## Not run: 
## Requires Mongo test database to be set up.

m1 <- P4Message("Fred","Task1","PP","Task Done",
                details=list("Selection"="B"))
m2 <- P4Message("Fred","Task1","EI","New Obs",
                details=list("isCorrect"=TRUE,"Selection"="B"))
m3 <- P4Message("Fred","Task1","EA","New Stats",
                details=list("score"=1,"theta"=0.12345,"noitems"=1))

testcol <- mongo("Messages",
                 url="mongodb://test:secret@127.0.0.1:27017/test")
## Mongodb is the protocol
## user=test, password =secret
## Host = 127.0.0.1 -- localhost
## Port = 27017 -- Mongo default
## db = test
## collection = Messages
## collection = Messages
## Execute in Mongo Shell
## db.createUser({
## ... user: "test",
## ... pwd: "secret",
## ... roles: [{role: "readWrite", db: "test"}]
## ... });



m1 <- saveRec(m1,testcol)
m2 <- saveRec(m2,testcol)
m3 <- saveRec(m3,testcol)

m1@data$time <- list(tim=25.4,units="secs")
m1 <- saveRec(m1,testcol)

## Note use of oid keyword to fetch object by Mongo ID.
m1a <- getOneRec(buildJQuery("_id"=c(oid=m1@"_id")),testcol,parseMessage)
stopifnot(all.equal(m1,m1a))

m123 <- getManyRecs(buildJQuery(uid="Fred"),testcol,parseMessage)
m23 <- getManyRecs(buildJQuery(uid="Fred",sender=c("EI","EA")),
                   testcol,parseMessage)
m321 <- getManyRecs(buildJQuery(uid="Fred",timestamp=c(lte=Sys.time())),
            testcol,parseMessage,sort=c(timestamp=-1))
getManyRecs(buildJQuery(uid="Fred",
                        timestamp=c(gte=Sys.time()-as.difftime(1,units="hours"))),
                        testcol,parseMessage)

## End(Not run)


[Package Proc4 version 0.4-7 Index]