How to query to get the document

118 views
Skip to first unread message

Raheel Mateen

unread,
Sep 24, 2014, 7:57:12 AM9/24/14
to mobile-c...@googlegroups.com
I follow the following to start the couch db on android
http://docs.couchbase.com/couchbase-lite/cbl-android/cbl-android-1.0b2/#developing-apps

but i actually not understand how to query in couch db for example if i make a document in a db so how should i access that document with the help of query.

Traun Leyden

unread,
Sep 24, 2014, 4:53:31 PM9/24/14
to mobile-c...@googlegroups.com

Can you provide more details of what you are trying to do?

Are you trying to load a single document using it's document id? 

Or are you trying to query a subset of the documents based on some query criteria?



--
You received this message because you are subscribed to the Google Groups "Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mobile-couchbase/4b4680bf-3c26-411a-b92a-85e1d97c25ca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Thiago Alencar

unread,
Sep 25, 2014, 10:01:10 AM9/25/14
to mobile-c...@googlegroups.com
Hi Raheel.

I skimmed the android documentation you mentioned,  I believe you'll understand it better after reading my tutorial : http://ti.eng.br/beginning-with-couchbase-lite-on-ios/

The concepts are exactly the same, the only thing that changes is basically the programming language. But the method names are pretty consistent so you should understand it. 
You can also skip the "Model" section of my tutorial as AFAIK that is only applying for iOS at the moment. But all the rest is the same.

BR,
Thiago

Raheel Mateen

unread,
Sep 29, 2014, 3:46:40 AM9/29/14
to mobile-c...@googlegroups.com
thank you for your replies i need to know one more thing i am still confuse that in what format the document data is stored for example

// create an empty document
com.couchbase.lite.Document document = database.createDocument();

// create an object that contains data for a document
Map<String, Object> docContent = new HashMap<String, Object>();
docContent.put("message", "Hello Couchbase Lite 1");
docContent.put("name", "raheel");
docContent.put("lastUpdated", this.date());

// Put the properties of Document
  try {
document.putProperties(properties);
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}

i need to know how the data looks after storing in the document .

Jens Alfke

unread,
Sep 29, 2014, 7:32:38 AM9/29/14
to mobile-c...@googlegroups.com
It's JSON.

—Jens

Thiago Alencar

unread,
Sep 29, 2014, 7:39:19 AM9/29/14
to mobile-c...@googlegroups.com
And by the way, if you have a couchbase server you can see the raw data e.g. using the administration panel at port 8091.

Raheel Mateen

unread,
Sep 29, 2014, 8:04:07 AM9/29/14
to mobile-c...@googlegroups.com
thank you one more thing what api should i use for testing syncing the document to server i am confused what this code do :

URL syncUrl;
        try {
            syncUrl = new URL(SYNC_URL);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }

        Replication pullReplication = database.createPullReplication(syncUrl);
        pullReplication.setContinuous(true);

        Replication pushReplication = database.createPushReplication(syncUrl);
        pushReplication.setContinuous(true);

        pullReplication.start();
        pushReplication.start();

        pullReplication.addChangeListener(this);
        pushReplication.addChangeListener(this);

On Wednesday, September 24, 2014 4:57:12 PM UTC+5, Raheel Mateen wrote:

Raheel Mateen

unread,
Sep 29, 2014, 8:12:04 AM9/29/14
to mobile-c...@googlegroups.com
i just installed couchbase server and make a new data bucket and name it syn_gateway so what is the url to send over that gateway ?


On Wednesday, September 24, 2014 4:57:12 PM UTC+5, Raheel Mateen wrote:

Thiago Alencar

unread,
Sep 29, 2014, 8:33:54 AM9/29/14
to mobile-c...@googlegroups.com
Hi Raheel, you might want to install the sync gateway, and configure it to use your couchbase server's bucket. Then in your Couchbase Lite you point to the gateway's URL.

You need the gateway because exposing a database directly to the application not the way to go (for many reasons).

References: 



BR,
Thiago

Raheel Mateen

unread,
Sep 29, 2014, 8:46:19 AM9/29/14
to mobile-c...@googlegroups.com
Thanks thiago,
i installed sync_gateway and now as per document 
http://developer.couchbase.com/mobile/develop/guides/sync-gateway/getting-started-with-sync-gateway/starting-sync-gateway/index.html

how should i start the gateway ? is there config.json file which i write and run in terminal or else ?


On Wednesday, September 24, 2014 4:57:12 PM UTC+5, Raheel Mateen wrote:

Thiago Alencar

unread,
Sep 29, 2014, 8:53:22 AM9/29/14
to mobile-c...@googlegroups.com
This is how I start mine:

./sync_gateway -url http://127.0.0.1:8091 -bucket sync_gw -pretty sync_function.js

If you don't specify the sync function, the default one is used:

function (doc) {
   channel(doc.channels);
}

You can see all the available commands as usual with  ./sync_gateway --help

Then you just need to make sure that your firewall makes the gateway's port accessible.

BR,
Thiago

Thiago Alencar

unread,
Sep 29, 2014, 8:58:02 AM9/29/14
to mobile-c...@googlegroups.com
By the way, maybe this configuration file helps you get started:

{

   "interface":":4984",

   "adminInterface":":4985",

   "log": ["REST","CRUD", "CRUD+", "HTTP", "HTTP+", "Access", "Cache", "Shadow", "Shadow+", "Changes", "Changes+"],

   "databases":{

      "sync_gw":{

         "server":"http://localhost:8091",

         "bucket":"sync_gw"

         "sync":`

// sync function

function(doc) { channel(doc.channels);  }

`     }

   }

}


That is expecting a bucket in your database named "sync_gw", and exposes the db with the same name. So you would point to it like: http://yourserever:4984/sync_gw  in your app.

BR,
Thiago

Raheel Mateen

unread,
Sep 29, 2014, 9:26:11 AM9/29/14
to mobile-c...@googlegroups.com
Thanks again Thiago ,

one more thing if you dont mind you said 
http://yourserever:4984/sync_gw  in your app. So i use localhost instead of yourserver?


On Wednesday, September 24, 2014 4:57:12 PM UTC+5, Raheel Mateen wrote:

Thiago Alencar

unread,
Sep 29, 2014, 9:46:56 AM9/29/14
to mobile-c...@googlegroups.com
No problem. 

> So i use localhost instead of yourserver?

Yes.

BR!

Raheel Mateen

unread,
Sep 29, 2014, 10:19:50 AM9/29/14
to mobile-c...@googlegroups.com
One more thing i created config.json file

here is the code:
 {
   "interface":":4984",
   "adminInterface":":4985",
   "log":["REST"],
   "databases":{
      "sync_gateway":{
         "server":"http://localhost:8091",
         "bucket":"sync_gateway",
         "sync":`function(doc) {channel(doc.channels);}`
      }
   }
}

so where should i placed that config file to run like this
https://groups.google.com/forum/#!topic/mobile-couchbase/NX3Q29uRpAQ

On Wednesday, September 24, 2014 4:57:12 PM UTC+5, Raheel Mateen wrote:

Thiago Alencar

unread,
Sep 29, 2014, 10:35:35 AM9/29/14
to mobile-c...@googlegroups.com
I'm not sure I understand your question: you can place the file anywhere and then give its path as your last parameter for the gateway start command.

Raheel Mateen

unread,
Sep 29, 2014, 11:07:48 AM9/29/14
to mobile-c...@googlegroups.com
yes its working fine 

but when i use this url 
public static final String SYNC_URL = "http://localhost:4984/sync_gateway";

and sync the documnet 

URL syncUrl;
        try {
            syncUrl = new URL(SYNC_URL);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }

        Replication pullReplication = database.createPullReplication(syncUrl);
        pullReplication.setContinuous(true);

        Replication pushReplication = database.createPushReplication(syncUrl);
        pushReplication.setContinuous(true);

        pullReplication.start();
        pushReplication.start();

        pullReplication.addChangeListener(this);
        pushReplication.addChangeListener(this);

so its generating the error connection to "http://localhost:4984 refused

On Wednesday, September 24, 2014 4:57:12 PM UTC+5, Raheel Mateen wrote:

Traun Leyden

unread,
Sep 29, 2014, 11:39:57 AM9/29/14
to mobile-c...@googlegroups.com
You need to use 10.0.2.2:4984 if you are on default android emulator.  

Or are you testing from a device?

--
You received this message because you are subscribed to the Google Groups "Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.

Raheel Mateen

unread,
Sep 29, 2014, 11:53:47 AM9/29/14
to mobile-c...@googlegroups.com
i am testing on galaxy s3 (Device)


On Wednesday, September 24, 2014 4:57:12 PM UTC+5, Raheel Mateen wrote:

Traun Leyden

unread,
Sep 29, 2014, 12:23:24 PM9/29/14
to mobile-c...@googlegroups.com
If your sync gw is running in cloud with public, use the public ip.

If sync gw is running on wifi LAN, use LAN ip of machine running sync gw.
--
You received this message because you are subscribed to the Google Groups "Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.

Thiago Alencar

unread,
Sep 29, 2014, 4:14:37 PM9/29/14
to mobile-c...@googlegroups.com
Now I understood your question. I'm sorry I thought you were talking about the config file. To clarify again: the contents going **inside** the sync gateway's configuration file, tells the sync gateway where your database is. Since my couchbase server is in the same machine as my sync gateway, I could put 127.0.0.1 or localhost there (inside the sync gw's configuration file).

Now this: http://yourserever:4984/sync_gw is the link is pointing to your gateway (from your mobile device), so that your android client application knows where to find your gateway. See:

Mobile app (with couchbase lite)  >>>> gateway >>>> couchbase server.

But this is well explained in the documentation !!  The rest is just TCP networking as usual: your android device just needs to be able to reach your sync gateway (but you should not have  something like this: http://localhost:4984/sync_gw in your mobile device, since the device doesn't need to sync with itself > the database is already there ;)

BR,
Thiago

Traun Leyden

unread,
Sep 29, 2014, 4:26:02 PM9/29/14
to mobile-c...@googlegroups.com
@Thiago,

This feels like a few discussion threads are getting crossed or something .. either that or I'm totally confused.

My most recent post was in response to:

but when i use this url 
public static final String SYNC_URL = "http://localhost:4984/sync_gateway";
and sync the documnet 
URL syncUrl;
        try {
            syncUrl = new URL(SYNC_URL);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
        Replication pullReplication = database.createPullReplication(syncUrl);
        pullReplication.setContinuous(true);

which was a Couchbase Lite Android app trying to do a sync with Sync Gateway.  

In this case, use "localhost" won't work in any situation, because the two common cases:

* Run Android app in emulator --> Requires the use of the special 10.0.2.2 address if Sync Gw on workstation, or public IP of machine if running in the cloud
* Run Android app on device --> Requires the use of the LAN IP or public IP of machine running sync gateway

and none of this has to do with the configuration of Sync Gateway to find out the address of Couchbase Server in order to communicate with it.



But this is well explained in the documentation !!  The rest is just TCP networking as usual: your android device just needs to be able to reach your sync gateway (but you should not have  something like this: http://localhost:4984/sync_gw in your mobile device, since the device doesn't need to sync with itself > the database is already there ;)


The only way you'd want localhost:4984 from an Android app is if Sync Gateway itself was somehow running on the Android emu/device.  SInce Sync Gateway doesn't run on Android, that setting doesn't make sense.

 
BR,
Thiago
 

On Monday, September 29, 2014 6:23:24 PM UTC+2, Traun Leyden wrote:
If your sync gw is running in cloud with public, use the public ip.

If sync gw is running on wifi LAN, use LAN ip of machine running sync gw.

On Sep 29, 2014, at 8:53 AM, Raheel Mateen <raheel...@riksof.com> wrote:

i am testing on galaxy s3 (Device)

On Wednesday, September 24, 2014 4:57:12 PM UTC+5, Raheel Mateen wrote:
I follow the following to start the couch db on android
http://docs.couchbase.com/couchbase-lite/cbl-android/cbl-android-1.0b2/#developing-apps

but i actually not understand how to query in couch db for example if i make a document in a db so how should i access that document with the help of query.

--
You received this message because you are subscribed to the Google Groups "Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mobile-couchbase/bcd8fd9a-2ba4-4817-998b-94edd4233bf9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.

Thiago Alencar

unread,
Sep 29, 2014, 6:21:52 PM9/29/14
to mobile-c...@googlegroups.com
@Traun,

You're right on your responses - its me who confused because at first we were talking about the sync gateway, but then later I realised that the Raheel's second question was related to the mobile app side URL and not the gateway, so I made another answer to correct myself - it just confirms what you said. I think it should all be clear now.

BR,
Thiago

Raheel Mateen

unread,
Sep 30, 2014, 2:45:02 AM9/30/14
to mobile-c...@googlegroups.com
I got the following error while connecting my app to sync_gateway :

connection to http://127.0.0.1:4984 refused

but when i run the followng in my local machine browser

so i get the response this:
{"committed_update_seq":6,"compact_running":false,"db_name":"sync_gateway","disk_format_version":0,"instance_start_time":1412058208385802,"purge_seq":0,"update_seq":6}

so why should the connection refused ?

On Wednesday, September 24, 2014 4:57:12 PM UTC+5, Raheel Mateen wrote:

Raheel Mateen

unread,
Sep 30, 2014, 3:10:09 AM9/30/14
to mobile-c...@googlegroups.com
Yes its done :D


On Wednesday, September 24, 2014 4:57:12 PM UTC+5, Raheel Mateen wrote:

Traun Leyden

unread,
Sep 30, 2014, 11:21:07 AM9/30/14
to mobile-c...@googlegroups.com
On Mon, Sep 29, 2014 at 11:45 PM, Raheel Mateen <raheel...@riksof.com> wrote:
so why should the connection refused ?

As I mentioned earlier in the thread:

In this case, use "localhost" won't work in any situation, because the two common cases:

* Run Android app in emulator --> Requires the use of the special 10.0.2.2 address if Sync Gw on workstation, or public IP of machine if running in the cloud
* Run Android app on device --> Requires the use of the LAN IP or public IP of machine running sync gateway

 
On Wednesday, September 24, 2014 4:57:12 PM UTC+5, Raheel Mateen wrote:
I follow the following to start the couch db on android
http://docs.couchbase.com/couchbase-lite/cbl-android/cbl-android-1.0b2/#developing-apps

but i actually not understand how to query in couch db for example if i make a document in a db so how should i access that document with the help of query.

--
You received this message because you are subscribed to the Google Groups "Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.

Jens Alfke

unread,
Sep 30, 2014, 1:37:09 PM9/30/14
to mobile-c...@googlegroups.com

On Sep 29, 2014, at 11:45 PM, Raheel Mateen <raheel...@riksof.com> wrote:

I got the following error while connecting my app to sync_gateway :

connection to http://127.0.0.1:4984 refused 

Raheel, it sounds like you may not have a good enough understanding of IP addresses — 127.0.0.1 always refers to 'localhost', the same device making the connection. I'd suggest reading some introductory networking book; we're not really here to teach the basics.

—Jens
Reply all
Reply to author
Forward
0 new messages