Description Usage Arguments Details Value See Also Examples
Find the first record in a collection that matches a given query.
1 2 | mongo.find.one(mongo, ns, query = mongo.bson.empty(),
fields = mongo.bson.empty())
|
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, Alternately, |
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, Alternately, |
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.
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.
mongo.find
,
mongo.index.create
,
mongo.insert
,
mongo.update
,
mongo.remove
,
mongo,
mongo.bson.
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"))
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.