开发者

CouchDB simple find

开发者 https://www.devze.com 2023-03-06 22:49 出处:网络
I have some couchDB database. and i want, as in mongodb, find one item. something like db.find({user : \"John\"})

I have some couchDB database. and i want, as in mongodb, find one item. something like db.find({user : "John"}) That the开发者_StackOverflow中文版 easiest way to do it?


If you have your queries predefined, you can use views to query your database.

There is also the ability to use temporary views for ad-hoc searches, but they are never recommended for production use because the index is not saved.

If you need something more along the lines of full-text search, check out couchdb-lucene.


What is your programming language of choice? CouchDB's API is HTTP based. Basically you could setup a view which uses the username as key and query that via HTTP request or with the help of a "driver" for your specific language.

Views are defined as map/reduce functions. An easy introduction can be found at the official wiki for example. Also taking a look at the CouchDB guide is a good place to start with.


I prefer using elasticsearch. It has a couchdb _river for integration. It will listen to _changes of couchdb, then fetch and index documents.

That way you get awesome power of elasticsearch (powered by lucene), with it's RESTful interfaces and clustering ability.

You get good separation of "searching" vs. your core documents. Which means you can index and search across different document stores.

Admittedly you don't get a nice small all in one package, but for flexibility for my use cases it wins hands down.


I have a new project to do this: http://github.com/iriscouch/query_couchdb

(Hopefully I can add an intro and documentation today.)

The idea is to copy the Google App Engine Python API.

new Query("User")
    .filter("name =", "John")
    .order('-age')
    .get(function(er, view) {
        if(er)
            throw(er);

        console.log("Got " + view.rows.length + " rows!");
        for(var a = 0; a < view.rows.length; a++) {
            var row = view.rows[a];
            console.log("Row " + a + " = " + JSON.stringify(row));
        }
});

Unfortunately it is missing unit tests and examples, but I am already using this in production.


There is an initiative to implement a mongo-like find with the query syntax offered by mongo db. Cloudant announced the initiative and they started contributing through mango, a MongoDB inspired query language interface for Apache CouchDB.

The Cloudant project should allow this type of queries find({user : "John"}), find({user:{$in : ["Doe", "Smith"]}}) or find({"age": {"$gt": 21}}) for age > 21

A similar alternative pouchdb-find is also being developed for pouch db.

0

精彩评论

暂无评论...
验证码 换一张
取 消