Created
July 7, 2016 04:49
-
-
Save ahgood/fa6b2d6daa91cda2d192985e1a20e805 to your computer and use it in GitHub Desktop.
Query and update document in Cloudant
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Learn more: https://github.com/cloudant/nodejs-cloudant | |
//Before querying, you need to create index first | |
var Cloudant = require('cloudant'); | |
var cloudant = Cloudant({account: 'ACCOUNT', key: 'API-KEY', password: 'PASSWORD'}); | |
var db = cloudant.db.use('DB1'); | |
var index_user = {name:'user', type:'json', index:{fields:['user']}} | |
db.index(index_user, function(er, response) { | |
if (er) throw er; | |
console.log('Index creation result: %s', response.result); | |
}); | |
//Query document by _id | |
var Cloudant = require('cloudant'); | |
var cloudant = Cloudant({account: 'ACCOUNT', key: 'API-KEY', password: 'PASSWORD'}); | |
var db = cloudant.db.use('DB1'); | |
db.find({selector:{ "_id":"testid" }}, function(err, result) { | |
if (err) return console.log(err.message); | |
console.log(result); | |
}); | |
//Update document by _id | |
var Cloudant = require('cloudant'); | |
var cloudant = Cloudant({account: 'ACCOUNT', key: 'API-KEY', password: 'PASSWORD'}); | |
var db = cloudant.db.use('DB1'); | |
db.find({selector:{ "_id":"testid" }}, function(err, result) { | |
if (err) return console.log(err.message); | |
console.log('Find completed: ' + JSON.stringify(result)); | |
db.insert({ _id: 'testid', user: 'ahgood', _rev: result.docs[0]._rev }, function(err, data) { | |
if (err) return console.log(err.message); | |
console.log('Insert completed: ' + data); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment