mongo.find.one: Find one record in a collection

Description Usage Arguments Details Value See Also Examples

View source: R/mongo_find.R

Description

Find the first record in a collection that matches a given query.

Usage

1
2

Arguments

mongo

(mongo) A mongo connection object.

ns

(string) The namespace of the collection from in which to find a record.

query

(mongo.bson) The criteria with which to match the record that is to be found. The default of mongo.bson.empty() will cause the the very first record in the collection to be returned.

Alternately, query may be a list which will be converted to a mongo.bson object by mongo.bson.from.list().

Alternately, query may be a valid JSON character string which will be converted to a mongo.bson object by mongo.bson.from.JSON().

fields

(mongo.bson) The desired fields which are to be returned frtom the matching record. The default of mongo.bson.empty() will cause all fields of the matching record to be returned; however, specific fields may be specified to cut down on network traffic and memory overhead.

Alternately, fields may be a list which will be converted to a mongo.bson object by mongo.bson.from.list().

Alternately, fields may be a valid JSON character string which will be converted to a mongo.bson object by mongo.bson.from.JSON().

Details

This is a simplified version of mongo.find() which eliminates the need to step through returned records with a cursor.

See http://www.mongodb.org/display/DOCS/Querying.

Value

NULL if no record matching the criteria is found; otherwise,

(mongo.bson) The matching record/fields.

Note that NULL may also be returned if a database error occurred (when a badly formed query is used, for example). mongo.get.server.err and mongo.get.server.err.string may be examined in that case.

See Also

mongo.find,
mongo.index.create,
mongo.insert,
mongo.update,
mongo.remove,
mongo,
mongo.bson.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
mongo <- mongo.create()
if (mongo.is.connected(mongo)) {
    buf <- mongo.bson.buffer.create()
    mongo.bson.buffer.append(buf, "name", "Jeff")
    query <- mongo.bson.from.buffer(buf)

    # find the first record where name is "Jeff"\
    #    in collection people of database test
    b <- mongo.find.one(mongo, "test.people", query)
    if (!is.null(b))
        print(b)

    buf <- mongo.bson.buffer.create()
    mongo.bson.buffer.append(buf, "_id", 1L)
    mongo.bson.buffer.append(buf, "age", 1L)
    fields <- mongo.bson.from.buffer(buf)

    # find the first record where name is "Jeff"
    #    in collection people of database test
    # return only the _id and age fields of the matched record
    b <- mongo.find.one(mongo, "test.people", query, fields)
    if (!is.null(b))
        print(b)

    # find the first record in collection cars of database test
    have.car <- !is.null(mongo.find.one(mongo, "test.cars"))

    # shorthand using a list:
    b <- mongo.find.one(mongo, "test.people", list(name="Jose"))
}

jonkatz2/rmongodb documentation built on May 19, 2019, 7:30 p.m.