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"))
}
Run the code above in your browser using DataLab