如何同时使用范围约束一个键并使用完全匹配约束另一个键?

问题描述 投票:0回答:1

我创建了一个测试Node.js脚本,该脚本使用Nano生成一些示例数据文档,创建两个视图,并运行两个测试查询。每个数据文档都有两个键:“a”和“b”。我希望我的查询产生所有文档,其中“a”介于1和3之间,“b”等于2.我测试了我在网上找到的使用startkey数组和endkey的视图/查询模式阵列。但是,当我在约束“b”之前约束“a”时,它的行为并不像预期的那样,但是当我在约束“a”之前约束“b”时,它似乎表现得像预期的那样。

为什么b_then_a视图似乎有效,但a_then_b视图不起作用?这种方法不正确吗?脚本及其输出如下。

var nano = require("nano")("http://HCOADAMM:HcoAdammRSM@localhost:5984");

let jasonDB = nano.db.use("jason");

const DESIGN_DOCUMENT_NAME = "findtest";

var testData = [
    { a: 1, b: 1 },
    { a: 1, b: 2 },
    { a: 1, b: 3 },
    { a: 1, b: 4 },
    { a: 2, b: 1 },
    { a: 2, b: 2 },
    { a: 2, b: 3 },
    { a: 2, b: 4 },
    { a: 3, b: 1 },
    { a: 3, b: 2 },
    { a: 3, b: 3 },
    { a: 3, b: 4 },
    { a: 4, b: 1 },
    { a: 4, b: 2 },
    { a: 4, b: 3 },
    { a: 4, b: 4 }
];


var shuffleArray = function(arrayIn) {
    var arrayInLength = arrayIn.length;
    var arrayOut = [];
    while(arrayInLength)
        arrayOut.push(arrayIn.splice(
            parseInt(Math.random() * (arrayInLength--)), 1
        )[0]);
    return arrayOut;
}

var createTestRecords = function() {

    var recordsShuffled = shuffleArray(testData);

    recordsShuffled.forEach(function(record) {
        jasonDB.insert(
            record,
            function(err, body) {
                if(err)
                    console.log(err);
                else
                    console.log("updated user doc " + JSON.stringify(body));
            }
        );    
    });
}

var createDesignDocument = function() {

    jasonDB.get("_design/" + DESIGN_DOCUMENT_NAME, {}, function(err, body, headers) {
        if(!err || err.error === "not_found") {

            var dbObject = new Object();
            dbObject._id = "_design/" + DESIGN_DOCUMENT_NAME;
            if(!err) {
                dbObject._rev = body._rev;
            }
            dbObject.language = "javascript";

            dbObject.views = {
                a_then_b: {
                    map: function(doc) {
                        emit([doc.a, doc.b]);
                    }
                },
                b_then_a: {
                    map: function(doc) {
                        emit([doc.b, doc.a]);
                    }
                },
            };

            jasonDB.insert(dbObject, function(err, body, header) {
                if(err) {
                    console.log("insert error:");
                    console.log(err);
                } else {
                    console.log("created " + "jason/_design/" + DESIGN_DOCUMENT_NAME);
                }
            })
        } else {
            console.log("get error:");
            console.log(err);
        }
    });


}

var queryTest = function() {

    jasonDB.view(
        DESIGN_DOCUMENT_NAME,
        "a_then_b",
        { startkey: [1, 2], endkey: [3, 2] },
        function(err, body) {
            if(err) {
                console.log(err);
            } else {
                console.log("a_then_b")
                body.rows.forEach(function(el) {
                    console.log(el);
                });
                console.log("body.rows.length = " + body.rows.length);
                console.log("");
            }
        }
    );

    jasonDB.view(
        DESIGN_DOCUMENT_NAME,
        "b_then_a",
        { startkey: [2, 1], endkey: [2, 3] },
        function(err, body) {
            if(err) {
                console.log(err);
            } else {
                console.log("b_then_a")
                body.rows.forEach(function(el) {
                    console.log(el);
                });
                console.log("body.rows.length = " + body.rows.length);
            }
        }
    );
}

//createTestRecords();
//createDesignDocument();
setTimeout(function() {
    queryTest();
}, 1000);

输出:

a_then_b
{ id: '812f16b3826569ec94eb35d087030d64',
  key: [ 1, 2 ],
  value: null }
{ id: '812f16b3826569ec94eb35d087030709',
  key: [ 1, 3 ],
  value: null }
{ id: '812f16b3826569ec94eb35d08702a846',
  key: [ 1, 4 ],
  value: null }
{ id: '812f16b3826569ec94eb35d087032077',
  key: [ 2, 1 ],
  value: null }
{ id: '812f16b3826569ec94eb35d08702fd89',
  key: [ 2, 2 ],
  value: null }
{ id: '812f16b3826569ec94eb35d08702caee',
  key: [ 2, 3 ],
  value: null }
{ id: '812f16b3826569ec94eb35d08702c32a',
  key: [ 2, 4 ],
  value: null }
{ id: '812f16b3826569ec94eb35d08702b358',
  key: [ 3, 1 ],
  value: null }
{ id: '812f16b3826569ec94eb35d087031386',
  key: [ 3, 2 ],
  value: null }
body.rows.length = 9

b_then_a
{ id: '812f16b3826569ec94eb35d087030d64',
  key: [ 2, 1 ],
  value: null }
{ id: '812f16b3826569ec94eb35d08702fd89',
  key: [ 2, 2 ],
  value: null }
{ id: '812f16b3826569ec94eb35d087031386',
  key: [ 2, 3 ],
  value: null }
body.rows.length = 3
couchdb
1个回答
0
投票

如果第二个键是索引中的第一个键,则只能执行此操作。因此,您需要尊重索引中的键,以便b首先被索引,而a,第二个。这将允许您通过[2,1]搜索一系列[2,3]

© www.soinside.com 2019 - 2024. All rights reserved.