Skip to content

Instantly share code, notes, and snippets.

@neumino
Created January 10, 2014 22:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save neumino/8363888 to your computer and use it in GitHub Desktop.
Save neumino/8363888 to your computer and use it in GitHub Desktop.
jakcharlton - IRC - 2014.01.10
// Retrieve all the documents where val == 1
// If no document is returned, insert {newVal: 2}
// Else update all the documents with {newVal: 2}
//
// Note: This query is not atomic
r.table("test").filter({val: 1}).count().do(function(numResults) {
return r.branch(
numResults.eq(0),
r.table("test").insert({newVal: 2}),
r.table("test").filter({val: 1}).update({newVal: 2})
)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment