what principle should I following on developing view both couchbase server and couchbase Lite?

25 views
Skip to first unread message

atom992

unread,
Jul 3, 2015, 12:30:29 PM7/3/15
to mobile-c...@googlegroups.com
I have two docs like this:
{"_id":"doc1","type":"TYPE1","key":"200","create_time":201506231000,"field1":"a","field2":"b"}
{"_id":"doc2","type":"TYPE2","key":"200","create_time":201506241200,"field3":"c"}

My Map function like this:
function (doc,meta) {
 
if (doc.type == "TYPE1" && doc.key && doc.create_time && doc.field1 && doc.field2){
    emit
(doc.key,{"create_time":doc.create_time,"type":doc.type,"field": {"field1":doc.field1,"field2":doc.field2}});
 
} else if (doc.type == "TYPE2" && doc.key && doc.create_time && doc.field3){
    emit
(doc.key,{"create_time":doc.create_time,"type":doc.type,"field": {"field3":doc.field3}});
 
}
}


so I can get the following result by key = "200" ( which I want to get)

{"total_rows":2,"rows":[
{"id":"doc1","key":"200","value":{"create_time":201506231000,"type":"TYPE1","field":{"field1":"a","field2":"b"}}},
{"id":"doc2","key":"200","value":{"create_time":201506241200,"type":"TYPE2","field":{"field3":"c"}}}
]
}

Is this function be the correct usage ? or what principle should I following on developing view both couchbase server and couchbase Lite? 
and If I want to sort by create_time and using paging , what should I do?

Jens Alfke

unread,
Jul 3, 2015, 1:06:46 PM7/3/15
to mobile-c...@googlegroups.com

On Jul 3, 2015, at 9:30 AM, atom992 <yangzi...@gmail.com> wrote:

or what principle should I following on developing view both couchbase server and couchbase Lite? 

The difference between the two is the location of the metadata.

On Couchbase Server, the map function takes a second ‘meta’ parameter, and the document ID is meta.id. Meta also has a few other fields like expiration that aren’t in Couchbase Lite.

On Couchbase Lite, there is no second parameter, and the document id is doc._id. Other metadata fields are _rev (revision ID) and _attachments (binary attachment metadata) that aren’t in Couchbase Server.

Other than that the logic of map functions and querying are the same, except that Couchbase Couchbase Server defaults to stale=true (which means query results are only “eventually consistent” unless you explicitly specify stale=false in the query.)

It’s sort of confusing, I know! Both products are descendants of Apache CouchDB, but Couchbase Lite stuck more closely to CouchDB, while Couchbase Server made a lot of changes to integrate better with its memcached and membase heritage.

—Jens

atom992

unread,
Jul 4, 2015, 12:08:52 AM7/4/15
to mobile-c...@googlegroups.com
Thank you very much.
and I want to know that:
Is this function be the correct usage ? 
If I want to sort by create_time and using paging , what should I do?

Jens Alfke

unread,
Jul 4, 2015, 2:31:26 AM7/4/15
to mobile-c...@googlegroups.com

On Jul 3, 2015, at 9:08 PM, atom992 <yangzi...@gmail.com> wrote:

If I want to sort by create_time and using paging , what should I do?

View indexes are always sorted by the key. If you want to sort by create_time, then create_time needs to be the key you emit, or the first element of an array key.

—Jens
Reply all
Reply to author
Forward
0 new messages