如何使用底座(CouchDB)将参数传递给视图 [英] How can I pass parameters to a view using cradle (CouchDB)
问题描述
使用摇篮,我如何能够将参数传递给CouchDB中的视图?
更新
Using cradle, how am I able to pass parameters to a view in CouchDB?
Update
说我想返回与_key
(默认值)以外的其他属性匹配的文档...
Say I want to return documents which match other properties than _key
(the default)...
// document format
{
_key,
postHeading,
postBody,
postDate
}
如果我想使文档与postHeading
属性匹配...该怎么办?视图将是什么样,如何将搜索字符串传递给该视图?
目前,我正在这样做...
What if I wanted to match documents against the postHeading
property... How would I go about this? What would the view look like, and how would I pass a search string to that view?
At the moment I'm doing this...
database.get("980f2ba66d5c8f9c91b9204a4d00022a", function (error, document)
{
});
我想访问一个视图,而不是40个字符长的自动生成的密钥,我想传递一个字符串,匹配另一个属性.
I would like to access a view instead, and instead of the 40 character long auto-generated key, I'd like to pass a string, matching another property.
与此类似的事物...
Something along the lines of this...
database.save("_design/posts", {
single: {
map: function (document)
{
if (document.postHeading == PARAMETER_PASSED_GOES_HERE)
emit(null, document);
}
}
});
database.view("posts/single", function (error, documents)
{
});
推荐答案
如果要查询视图,请尝试将第二个参数作为选项对象传递给您的设置,例如:
If you are querying a view try to pass second parameter as options object with your settings, for example:
db.view('characters/all', {descending: true}, function (err, res) {
res.forEach(function (row) {
sys.puts(row.name + " is on the " +
row.force + " side of the force.");
});
});
另外请注意:
某些查询字符串参数的值 必须使用JSON编码.
Some query string parameters' values have to be JSON-encoded.
据我所知,您无法在CouchDB中创建视图,您无法在其中传递自定义参数,该参数将在map/reduce函数代码中使用.您必须从地图函数中发出键,并基于它们可以使用诸如startkey
和endkey
之类的参数查询视图.尝试查看数据库查询CouchDB方式文章.
As far as I know you can't create a view in CouchDB where you pass your custom parameter which will be used in map/reduce function code. You have to emit keys from your map function and based on them you can query the view with parameters like startkey
and endkey
. Try to look at Database Queries the CouchDB Way article.
这篇关于如何使用底座(CouchDB)将参数传递给视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!