3

So, what I'm doing should be really simple, and maybe it is and I'm just doing something wrong. I want to update an existing document in my database but I'm having some issues, can someone please advise?

Nano's Documentation states the following for insert:

db.insert(doc, [params], [callback])

Therefore, I should surely be able to do the following:

 var user = {
   'firstname' : 'my',
   'secondname' : 'name'
};

db.insert(user, {_rev: '2-cc5825485a9b2f66d79b8a849e162g2f'}, function(err, body) {}); 

However, whenever I try this, it creates an entirely new document. If I do the following then it will indeed update my document, but of course, with nothing in this document other than the _rev:

db.insert({_rev: '2-cc5825485a9b2f66d79b8a849e162g2f'}, function(err, body) {});

So the question is, how do I pass in my object and get it to update, rather than creating a new record?

the
  • 21,007
  • 11
  • 68
  • 101
Creights
  • 907
  • 1
  • 12
  • 25

4 Answers4

5
var user = {
   'firstname' : 'my',
   'secondname' : 'name',
   '_id': <id from prev object>,
   '_rev': <rev from prev object>
};

db.insert(user, function(err, body) {}); 

the _id and _rev are both required in order for the update to work. they should be in the object that you are sending also.

Nicholas Tsaoucis
  • 1,381
  • 2
  • 17
  • 39
3

The first argument in the db.insert(...) command is the document which you want to create/update. If you pass in a doc with a ._rev attribute, then it will replace the document with that same _rev in Cloudant with the doc passed in as the first argument of your db.insert(...). If the doc does not include a ._rev attribute, then Cloudant will create an entirely new document.

This explains the behavior you were experiencing in both the scenarios you tried. In order to make an update to your doc, make sure to include ._id and ._rev attributes, along with the rest of your doc's attributes when you use it as the first argument to your db.insert(...) function.

Jake Peyser
  • 1,180
  • 8
  • 17
0

Got it! Here's the solution:

db.get('user', { revs_info: true }, function(err, doc) {

   if (!err) {
     console.log(doc);

     doc.firstname = 'my';
     doc.secondname = 'name';

     db.insert(doc, doc.id, function(err, doc) {
        if(err) {
           console.log('Error inserting data\n'+err);
           return 500;
        }
        return 200;
     });
   }
});
Creights
  • 907
  • 1
  • 12
  • 25
  • I think you just need `db.insert(doc, function(err, doc) { ... });`. Also, `revs_info: true` isn't required for this example, all document `GET` requests will include the `_rev` field. `revs_info` will give you a bunch of metadata you probably don't need. – Mike Rhodes Jul 15 '15 at 17:46
0

First get the record id and rev id (_id,_rev)

const newData={email:"aftabfalak956@gmail.com",name:"Aftab Falak"}

cloudant.use("user").find({selector:{_id:"user_id"}}, (err, documents) => {

    var revision = documents.docs[0]._rev;
    const data={...documents.docs[0],...newData};
    
    cloudant.use("user").insert(data,{_rev:revision},function(err){
        if (!err) {
            console.log('success', 'The record was updated successfully');
        }
        else {
            console.log('failure', err);
        }
    });

});