0

我不知道如何在 CouchDB 中实现以下查询:

  • chr.letter中的两个对象/字典chr之间不一样且没有X,并且
  • 文档必须在 200000 - 2000000 范围内,但我想它必须在视图中startkey完成。endkey

示例输出可能如下所示:

  • {"_id":"7", "sub_name":"B01", "name":"A", "pos":828288, "s_type":1}
  • {"_id":"8", "sub_name":"B01", "name":"A", "pos":171878, "s_type":3} 在视图中将不满足范围条件
  • {"_id":"9", "sub_name":"B01", "name":"A", "pos":871963, "s_type":3}

文档 10 无效,因为 chr.no = 6 具有 chr.letter = X。文档 14 无效,因为 chr.no = 5 和 chr.no = 6 都具有相同的 chr.letter = G

该数据库包含以下文件:

{
  "_id":"10",
  "_rev":"3-5288068d2c4ef3e6a9d3f8ff4e3377dd",
  "sub_name":"B01",
  "name":"A",
  "pos":1932523,
  "s_type":1,
  "chr":[
      {
        "letter":"T",
        "no":4
      },
      {
        "letter":"A",
        "no":5
      },
      {
        "letter":"X",
        "no":6
      }
  ],
  "type":"Test"
}{
  "_id":"14",
  "_rev":"3-21300d06c31224416b8ff71b71b304d8",
  "sub_name":"B01",
  "name":"A",
  "pos":667214,
  "s_type":1,
  "chr":[
      {
        "letter":"T",
        "no":4
      },
      {
        "letter":"G",
        "no":5
      },
      {
        "letter":"G",
        "no":6
      }
  ],
  "type":"Test"
}{
  "_id":"7",
  "_rev":"2-1516ba547bdd21724158bc854f39f66b",
  "sub_name":"B01",
  "name":"A",
  "pos":828288,
  "s_type":1,
  "chr":[
      {
        "letter":"C",
        "no":5
      },
      {
        "letter":"T",
        "no":6
      }
  ],
  "type":"Test"
}{
  "_id":"8",
  "_rev":"2-750078ccc9e74616f33a2537e41b8414",
  "sub_name":"B01",
  "name":"A",
  "pos":171878,
  "s_type":3,
  "chr":[
      {
        "letter":"C",
        "no":5
      },
      {
        "letter":"T",
        "no":6
      }
  ],
  "type":"Test"
}{
  "_id":"9",
  "_rev":"2-3d68352a2d98c56fd322ae674fb7c38a",
  "sub_name":"B01",
  "name":"A",
  "pos":871963,
  "s_type":3,
  "chr":[
      {
        "letter":"A",
        "no":5
      },
      {
        "letter":"G",
        "no":6
      }
  ],
  "type":"Test"
}

上面的数据库是用下面的脚本创建的:

import couchdb

# $ sudo systemctl start couchdb
# http://localhost:5984/_utils/

server = couchdb.Server()
db = server.create("test")
# except couchdb.http.ResourceConflict:
#db = server["test"]

r = [["Test", "A", "B01", 828288,  1,    7, 'C', 5],
    ["Test", "A", "B01", 828288,  1,    7, 'T', 6],
    ["Test", "A", "B01", 171878,  3,    8, 'C', 5],
    ["Test", "A", "B01", 171878,  3,    8, 'T', 6],
    ["Test", "A", "B01", 871963,  3,    9, 'A', 5],
    ["Test", "A", "B01", 871963,  3,    9, 'G', 6],
    ["Test", "A", "B01", 1932523, 1,   10, 'T', 4],
    ["Test", "A", "B01", 1932523, 1,   10, 'A', 5],
    ["Test", "A", "B01", 1932523, 1,   10, 'X', 6],
    ["Test", "A", "B01", 667214,  1,   14, 'T', 4],
    ["Test", "A", "B01", 667214,  1,   14, 'G', 5],
    ["Test", "A", "B01", 667214,  1,   14, 'G', 6]]

# _id = None
for i in r:
    _id = str(i[5])
    doc = db.get(_id)
    if doc is None:
        doc = {
            'type': i[0],
            'name': i[1],
            'sub_name': i[2],
            'pos': i[3],
            's_type': i[4],
            '_id': _id,
            'chr':[]
        }

        doc['chr'].append({
            "letter":i[6],
            "no":i[7]
        })

    else:
        doc['chr'].append({
            "letter":i[6],
            "no":i[7]
        })

    db.save(doc)

如何实现上述查询,或者是否必须更改文档结构才能使查询成为可能?

4

1 回答 1

0

感谢https://stackoverflow.com/a/26436244/977828的解决方案

function(doc) {
    var judge = function (doc) {
        var unexpect = "X";
        var letter1 = unexpect, letter2 = unexpect;
        for (var i in doc.chr) {
            var chr = doc.chr[i];
            if (chr.no == 5) {
                letter1 = chr.letter;
            } else if (chr.no == 6) {
                letter2 = chr.letter;
            }
        }
        if (letter1 != letter2 && letter1 != unexpect && letter2 != unexpect) {
            return true;
        }
        return false;
    }; 
    if (judge(doc)) {
      emit(doc);
    }
}
于 2014-10-19T02:05:48.183 回答