Pouchdb 加入/link 个文件

Pouchdb join / link documents

我有 pouchdb/couchbase 数据以及已分配给用户的设备。 带有 _id 的设备和设备文档中有一个值为 user._id 的 checkedOutBy。在员工对象中有 user.name。当我获得设备对象时,如何获得 user.name 并与设备一起显示。

我已经搜索并阅读了有关使用 emit 的 map/reduce 并且没有掌握这个想法。我根据所学编写的代码是: 顺便说一句,我也在使用 Angularjs.

    field = "eq::"
this.getAllEquip = function(field){
            function map(doc) {
                if (doc.checkedOutBy !== undefined) {
                    emit(doc.checkedOutBy, {empName : doc.name});
                }
            }
            var result = database.query(map, {include_docs: true,
                            attachments: true,
                            startkey: field,
                            endkey: field + '\uffff'})
                .catch(function (err) {
                //error stuff here
            });
            return result
        };

我看不出这两个文档在哪里会聚在一起。我错过了什么?我的结果是空的。

装备json看起来像:

{checkedOutBy: "us::10015", 描述: "3P Microsoft Surface w/stylus & 电源线", equipId: "SUR1501", purchaseDate: "", rCost: 1000, id:"eq::10001"}

Emlpoyee json:

{"firstname":"Joe","gender":"male","lastname":"Blow","status":"active","title":"office","type":"userInfo","_id":"us::10015","_rev":"2-95e9f34784094104ad24bbf2894ae786"}

感谢您的帮助。

如果我正确理解了问题,这样的事情应该可行:

        //Sample Array of Objects with Equipment 
 var arr1=[{checkedout:"abc1",desc:"item1",id:1},
     {checkedout:"abc2",desc:"item2",id:2},
    {checkedout:"abc3",desc:"item3",id:3},
    {checkedout:"abc1",desc:"item1",id:4},
    {checkedout:"abc4",desc:"item3",id:5},
    {checkedout:"abc6",desc:"item3",id:6}];

//Sample array of objects with Employee - the "id" in arr2 matches with "checkout" in arr1  
            var arr2=[{name:"john",id:"abc1"},
    {name:"jack",id:"abc2"},
    {name:"alice",id:"abc3"},
    {name:"james",id:"abc4"}];

    var result = [];  //final result array

//loop through equipment array arr1
    arr1.forEach(function(obj) {

    var tempObj = obj;

    var checkedout_id=obj.checkedout;

    //do array.find which will return the first element in the array which satisfies the given function. This is absed on the assumption that that the id is unique for employee and there wont bwe multiple employees with same id (which is the "checkedout" field in equipment. If the employee is not found, it will return undefined.
    var foundname = arr2.find(function(obj) {

    if (obj.id  == checkedout_id)

     return obj.name

    })

  //Create the object to be inserted into the final array by adding a new key called "name", based on the result of above find function  
    if (foundname != undefined) {

    tempObj.name=foundname.name

    }
    else {

    tempObj.name = "Not found";

    }


    result.push(tempObj);


    })

这是我的 Pouchdb 解决方案,感谢 Vijay 引导我找到这个解决方案。 首先我得到我所有的设备。然后我使用 Vijay 的想法循环遍历数组并将名称添加到对象并构建新数组。我发现需要进入 .doc。 obj.doc.checkedOutBy 和 tempObj.doc.name 中的对象的一部分来完成工作。

$pouchDB.getAllDocs('eq::').then(function(udata){
                var result = [];
                //loop through equipment array 
                udata.rows.forEach(function(obj) {
                    var tempObj = obj;
                    var checkedout_id=obj.doc.checkedOutBy;

                    if (checkedout_id != undefined) {
                        $pouchDB.get(checkedout_id).then(function(emp){
                            return emp.firstname + " " + emp.lastname
                        }).then(function(name){
                            tempObj.doc.name = name;
                        });
                    }
                    result.push(tempObj);
                })

在我的服务中我有:

this.get = function(documentId) {
            return database.get(documentId);
        };

和:

this.getAllDocs = function(field){
        return database.allDocs({
          include_docs: true,
          attachments: true,
          startkey: field,
          endkey: field + '\uffff'});
    };