mongo <- mongo.create()
if (mongo.is.connected(mongo)) {
# Count the number of records in collection people of database test
people.count <- mongo.count(mongo, "test.people")
print("total people")
print(people.count)
buf <- mongo.bson.buffer.create()
mongo.bson.buffer.append(buf, "age", 21L)
query <- mongo.bson.from.buffer(buf)
# Count the number of records in collection people of database test
# where age == 21
just.legal.count <- mongo.count(mongo, "test.people", query)
print("people of age 21")
print(just.legal.count)
buf <- mongo.bson.buffer.create()
mongo.bson.buffer.start.object(buf, "age")
mongo.bson.buffer.append(buf, "$gte", 21L)
mongo.bson.buffer.finish.object(buf)
query <- mongo.bson.from.buffer(buf)
# Count the number of records in collection people of database test
# where age >= 21
total.legal.count <- mongo.count(mongo, "test.people", query)
print("people of age 21 or greater")
print(total.legal.count)
# shorthand using a list:
ford.count <- mongo.count(mongo, "test.cars", list(make="Ford"))
}
Run the code above in your browser using DataLab