mongo <- mongo.create()
if (mongo.is.connected(mongo)) {
buf <- mongo.bson.buffer.create()
mongo.bson.buffer.append(buf, "age", 18L)
query <- mongo.bson.from.buffer(buf)
# Find the first 100 records
# in collection people of database test where age == 18
cursor <- mongo.find(mongo, "test.people", query, limit=100L)
# Step though the matching records and display them
while (mongo.cursor.next(cursor))
print(mongo.cursor.value(cursor))
mongo.cursor.destroy(cursor)
# shorthand: find all records where age=32, sorted by name,
# and only return the name & address fields:
cursor <- mongo.find(mongo, "test.people", list(age=32),
list(name=1L), list(name=1L, address=1L))
}
Run the code above in your browser using DataLab