Couchbase
  • Why NoSQL?
  • Couchbase Server
  • Download
  • Resources
  • Careers
Home | Forums | Couchbase | Couchbase Server 2.0

Querying on incremental field

1 reply [Last post]
  • Login or register to post comments
Mon, 11/05/2012 - 23:09
Idan
Offline
Joined: 05/18/2012
Groups: None

I wanted to use the counters for generating unique Ids. However, I've read on this page:

http://www.couchbase.com/docs/couchbase-devguide-2.0/cb-incr-decr.html

That:

"Both incr and decr are considered 'binary' methods in that they operate on binary data, not JSON documents. Because of this, keys used by incr and decr cannot be queried or indexed with Couchbase Server 2.0."

So how can I get data by user_id ? or any other query that needs to use the incremental key?

Am I missing something here - you already provided an example with user_id in that page. Again, what I want to achieve is to have an incremental ID for a document, I don't want to use GUID. When I asked you this question about IDs, you told me that I can use incremental numerical field. So if I can't query on it, what's the point?

Thanks

Top
  • Login or register to post comments
Tue, 11/06/2012 - 10:58
tgrall
Offline
Joined: 09/05/2012
Groups: None

Hello Idan,

I will try to explain how you can use incr/decr in your application, to generate unique ID

So if I understand correctly, what you have in mind is the "AUTO INCREMENT" of for example MySQL that allows you to say "auto increment this ID automatically", so each time you do an insert the column is incremented.

With Couchbase you have to take another approach (that is probably closer to the way you use SEQUENCES in Oracle... if you have used them in the past)

So with Couchbase you can use the following approach

1- You create a key/value pair that is used to manage the "increment". As you said the increment does operate on binary data, and not related to JSON at all.
So you have first to create the "sequence":

couchbase.set("employee_sequence",0); // set the sequence to 0

2- Each time you create a new employee you have to increment this "sequence"
var newValue = couchbase.increment("employee_sequence"); // this increment the "binary" value and assign it to the newValue variable

3- Now you can use the new value to create the employee:

couchbase.set( newValue , '{ "name":"John Doe", "id": newValue }' ); // you can set if you want the id into the document too if you want

Note: usually we create composite key instead of using only a number for example: "user::1" as key, using the following code:

couchbase.set( "user::"+ newValue , '{ "name":"John Doe", "id": newValue }' ); // you can set if you want the id into the document too if you want

4- You can now get the JSON document in a very efficient way using

couchbase.get("user:1");

Let me know if this helps you?

I am currently building a Node.js application that use exactly this approach to manage "chat messages":
look at https://github.com/tgrall/couchbase-chat/blob/master/app.js#L42

1 couchbase.incr("chat:msg_count", function (data, error, key, cas, value ) {
2 var messageKey = "chat:"+ value;
3 message.id = value;
4 couchbase.set(messageKey, JSON.stringify(message),function(err) { });
5 });

1-> I do increment the value of the key: "chat:msg_count" (the new value will be send to the parameter value)
2-> I create a new key using the new incremented value : var messageKey = "chat:"+ value;
4-> I use this key to store the message

Regards
Tug

__________________

Tug
@tgrall

Top
  • Login or register to post comments
  • Login or register to post comments
  • Login
  • Register

Company

  • About Us
  • Leadership
  • Customers
  • Partners
  • Contact Us

Product

  • Couchbase Server
  • Couchbase SDKs
  • Use Cases
  • Documentation
  • Forums

Open Source

  • Couchbase Project
  • Couchbase vs. CouchDB

Commercial

  • Subscriptions & Support
  • Training & Services

News

  • Blog
  • Newsletter
  • Press Releases
  • Buzz

Follow Us

    
  • Customer Login
  • Terms of Service
  • Privacy Policy
  • Trademark Policy
  • Site Map

© 2013 COUCHBASE All rights reserved.

Sign in to Couchbase Community

close
  • Create new account
  • Request new password
You are logging into the Forums, Wiki and Issue Tracker