mongo <- mongo.create()
if (mongo.is.connected(mongo)) {
ns <- "test.people"
buf <- mongo.bson.buffer.create()
mongo.bson.buffer.append(buf, "name", "Joe")
criteria <- mongo.bson.from.buffer(buf)
buf <- mongo.bson.buffer.create()
mongo.bson.buffer.start.object(buf, "$inc")
mongo.bson.buffer.append(buf, "age", 1L)
mongo.bson.buffer.finish.object(buf)
objNew <- mongo.bson.from.buffer(buf)
# increment the age field of the first record matching name "Joe"
mongo.update(mongo, ns, criteria, objNew)
buf <- mongo.bson.buffer.create()
mongo.bson.buffer.append(buf, "name", "Jeff")
criteria <- mongo.bson.from.buffer(buf)
buf <- mongo.bson.buffer.create()
mongo.bson.buffer.append(buf, "name", "Jeff")
mongo.bson.buffer.append(buf, "age", 27L)
objNew <- mongo.bson.from.buffer(buf)
# update the entire record to { name: "Jeff", age: 27 }
# where name equals "Jeff"
# if such a record exists; otherwise, insert this as a new reord
mongo.update(mongo, ns, criteria, objNew,
mongo.update.upsert)
# do a shorthand update:
mongo.update(mongo, ns, list(name="John"), list(name="John", age=25))
}
Run the code above in your browser using DataLab