使用Node.js从Couchdb中检索所有文档 [英] Retrieving all Documents from couchdb using Node.js

查看:234
本文介绍了使用Node.js从Couchdb中检索所有文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的测试应用程序,以尝试使用node.js和benchdb的功能,到目前为止,我还是很喜欢它,但是遇到了麻烦.我寻找并广泛,但似乎找不到答案.我的测试服务器(一个简单的通讯录)做两件事:

I am writing a simple test app to experiment with the functionality of node.js and couchdb, so far i am loving it, but i ran in a snag. i have looked for and wide but can't seem to find an answer. My test server(a simple address book) does 2 things:

  1. 如果用户转到localhost:8000/{id},则我的应用返回具有该ID的用户的名称和地址.
  2. 如果用户转到localhost:8000/,则我的应用程序需要返回一个列表,该列表包含超链接名称,并将其带到页面localhost:8000/{id}.
  1. if the user goes to localhost:8000/{id} then my app returns the name and address of the user with that id.
  2. if the user goes to localhost:8000/ then my app needs to return a list a names that are hyperlinks and takes them to the page localhost:8000/{id}.

我能够满足第一个要求.我似乎无法找到如何从我的Sofadb中检索所有名称的列表.这就是我需要帮助的地方.这是我的代码:

I was able to get the first requirement working. i cant not seem to find how to retrieve a list of all names from my couchdb. that is what i need help with. here is my code:

var http = require('http');
var cradle = require('cradle');
var conn = new(cradle.Connection)();
var db = conn.database('users');

function getUserByID(id) {
    var rv = "";

    db.get(id, function(err,doc) {
        rv = doc.name;
    rv += " lives at " + doc.Address;
});

return rv;
}

function GetAllUsers() {
var rv = ""
return rv;
}

var server =  http.createServer(function(req,res) {
res.writeHead(200, {'Content-Type':'text/plain'});
var rv = "" ;
var id = req.url.substr(1);

    if (id != "")
    rv = getUserByID(id);
else
    rv = GetAllUsers();

    res.end(rv);


});

server.listen(8000);
console.log("server is runnig");

如您所见,我需要填写GetAllUsers()函数.任何帮助,将不胜感激.预先感谢.

As you can see, I need to fill in the GetAllUsers() function. Any help would be appreciated. Thanks in advance.

推荐答案

您可以创建一个CouchDB视图,该视图将列出用户.以下是有关CouchDB视图的一些资源,您应该阅读这些资源,以便对该主题有更全面的了解:

You can create a CouchDB view which will list the users. Here are several resources on CouchDB views which you should read in order to get a bigger picture on this topic:

  • Introduction to CouchDB Views
  • Finding Your Data with Views
  • View Cookbook for SQL Jockeys
  • HTTP View API

因此,假设您的文档结构如下:

So let's say you have documents strucutred like this:

{
    "_id": generated by CouchDB,
    "_rev": generated by CouchDB,
    "type": "user",
    "name": "Johny Bravo",
    "isHyperlink": true
}

然后,您可以创建一个如下所示的CouchDB视图(地图部分):

Then you can create a CouchDB view (the map part) which would look like this:

// view map function definition
function(doc) {
    // first check if the doc has type and isHyperlink fields
    if(doc.type && doc.isHyperlink) {
        // now check if the type is user and isHyperlink is true (this can also inclided in the statement above)
        if((doc.type === "user") && (doc.isHyperlink === true)) {
            // if the above statements are correct then emit name as it's key and document as value (you can change what is emitted to whatever you want, this is just for example)
            emit(doc.name, doc);
        }
    }
}

创建视图后,您可以从node.js应用程序中对其进行查询:

When view is created you can query it from your node.js application:

// query a view
db.view('location of your view', function (err, res) {
    // loop through each row returned by the view
    res.forEach(function (row) {
        // print out to console it's name and isHyperlink flag
        console.log(row.name + " - " + row.isHyperlink);
    });
});

这只是一个例子.首先,我建议阅读以上资源,学习CouchDB视图的基础知识及其功能.

This is just an example. First I would recommend to go through the resources above and learn the basics of CouchDB views and it's capabilities.

这篇关于使用Node.js从Couchdb中检索所有文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆