Skip to content

Instantly share code, notes, and snippets.

@jakcharlton
Forked from neumino/gist:8363888
Created January 10, 2014 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakcharlton/8363967 to your computer and use it in GitHub Desktop.
Save jakcharlton/8363967 to your computer and use it in GitHub Desktop.
// 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