Copy document from one db to another (syncs to different instances of SG)

Basically, there are two db’s that sync with different Sync Gateway instances. Now from db1, i want to copy one document, make modifications to it and put it to db2.

My code gives me error: CouchbaseLiteException{CouchbaseLite,9,'Cannot operate on a document from another database

my code is simply (brief):

MutableDocument db1Doc = database1.getDocument('docid').toMutable();
//remove unneeded fields
db1Doc.remove("channels");
db1Doc.remove("syncTS");

//add DB2 needed fields.
db1Doc.setString("source", "copy");
//save to database two now
database2.save(db1Doc);

Can we please get some additional information?

  • what Couchbase platform are you using
  • what version of Couchbase are you using?
  • can we see the entire stack trace, please

Thanks.

Couchbase lite Android 2.8.0

the stack trace is rather short but here it is:

2022-02-01 20:00:09.707 6535-6744/com.app W/System.err: CouchbaseLiteException{CouchbaseLite,9,'Cannot operate on a document from another database.
2022-02-01 20:00:09.722 6535-6744/com.app W/System.err: at com.couchbase.lite.AbstractDatabase.prepareDocument(AbstractDatabase.java:1307)
2022-02-01 20:00:09.722 6535-6744/com.app W/System.err: at com.couchbase.lite.AbstractDatabase.saveInternal(AbstractDatabase.java:1525)
2022-02-01 20:00:09.722 6535-6744/com.app W/System.err: at com.couchbase.lite.AbstractDatabase.save(AbstractDatabase.java:458)
2022-02-01 20:00:09.722 6535-6744/com.app W/System.err: at com.couchbase.lite.Database.save(Database.java:28)
2022-02-01 20:00:09.723 6535-6744/com.app W/System.err: at com.couchbase.lite.AbstractDatabase.save(AbstractDatabase.java:442)
2022-02-01 20:00:09.723 6535-6744/com.app W/System.err: at com.couchbase.lite.Database.save(Database.java:28)
2022-02-01 20:00:09.723 6535-6744/com.app W/System.err: at com.app.DB.copyDocument(DB.java:573)

Ok. The error message means exactly what it says. You’ve got a document from database1 and you are trying to save it in database2. Can’t do that. You want something like: ```

MutableDocument doc = new MutableDocument(database1.getDocument('docid').toMap());
//remove unneeded fields
doc.remove("channels");
doc.remove("syncTS");

//add DB2 needed fields.
doc.setString("source", "copy");
//save to database two now
database2.save(doc);