我在 CouchDB 中存储了以下文档:
{
"_id":"1",
"_rev":"1-e3ff3eb51ccf90e0446ef086fcc6a06c",
"sub_name":"A01",
"type":"Test",
"name":"A",
"pos":828288
}{
"_id":"2",
"_rev":"1-e3ff3eb51ccf90e0446ef086fcc6a06c",
"sub_name":"A02",
"type":"Test",
"name":"A",
"pos":828288
}{
"_id":"3",
"_rev":"1-ef83655c51f64daa4c074fed9beb8234",
"sub_name":"B01",
"type":"Test",
"name":"B",
"pos":171878
}{
"_id":"4",
"_rev":"1-ef83655c51f64daa4c074fed9beb8234",
"sub_name":"B02",
"type":"Test",
"name":"B",
"pos":171878
}{
"_id":"5",
"_rev":"1-52b91ba1577a11bf410999ceb2577206",
"sub_name":"C01",
"type":"Test",
"name":"C",
"pos":871963
}{
"_id":"6",
"_rev":"1-52b91ba1577a11bf410999ceb2577206",
"sub_name":"C02",
"type":"Test",
"name":"C",
"pos":871963
}{
"_id":"7",
"_rev":"1-807f46b501b237a6e0f2ba71ffd7f194",
"sub_name":"D01",
"type":"Test",
"name":"D",
"pos":1932523
}{
"_id":"8",
"_rev":"1-807f46b501b237a6e0f2ba71ffd7f194",
"sub_name":"D02",
"type":"Test",
"name":"D",
"pos":1932523
}
更新 我想为用户提供通过页面上的选择框选择不同文档的选项,其中可以选择类型、名称和子名称。但是,我找不到任何动态执行查询的示例。像这样:
function(doc, type, name, sub_name) {
if (doc.type == type && doc.name == name && doc.sub_name == sub_name) {
emit(doc._id, doc.pos);
}
}
我是否需要为所有 doc.type、doc.name 和 doc.sub_name 组合创建一个类似于以下 View 的单独 View ,或者是否有更好的方法来实现?
function(doc) {
if (doc.type == "Test" && doc.name == "A" && doc.sub_name == "A01") {
emit(doc._id, doc.pos);
}
}
function(doc) {
if (doc.type == "Test" && doc.name == "A" && doc.sub_name == "A02") {
emit(doc._id, doc.pos);
}
}
function(doc) {
if (doc.type == "Test" && doc.name == "B" && doc.sub_name == "B01") {
emit(doc._id, doc.pos);
}
}
...
最佳答案
您无法按照您尝试的方式动态查询 CouchDB - View 是在索引时(即创建或更新文档时)构建的,并且只能通过传递键或键范围来查询。不可能根据任何给定的文档属性进行动态查询。
您可以做的是发出复合键:
function(doc) {
emit([doc.type, doc.name, doc.sub_name], doc.pos);
}
然后您可以通过传递数组作为键来查询 View :
/.../my_view?key=["Test","B","B01"]
关于javascript - 是否有必要在 CouchDB 中创建所有组合的单独 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26427552/