Description Usage Arguments Details See Also Examples
View source: R/mongo_records.R
Perform an update on a collection.
1 | mongo.update(mongo, ns, criteria, objNew, flags = 0L)
|
mongo |
(mongo) a mongo connection object. |
ns |
(string) namespace of the collection to which to update. |
criteria |
(mongo.bson) The criteria with which to match records that are to be updated. Alternately, Alternately, |
objNew |
(mongo.bson) The replacement object. Alternately, Alternately, |
flags |
(integer vector) A list of optional flags governing the operation:
|
See http://www.mongodb.org/display/DOCS/Updating.
mongo,
mongo.bson,
mongo.insert
,
mongo.find
,
mongo.find.one
,
mongo.remove
.
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 31 32 33 34 35 | 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))
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.