[英]Google Datastore NodeJS combine (union) multiple sets of keys only results
I am working with NodeJS on Google App Engine with the Datastore database. 我正在使用带有数据存储数据库的Google App Engine上的NodeJS。
Note that this question is an extension of (not a duplicate) my original post . 请注意,这个问题是我原帖的扩展(不是重复)。
Due to the fact that Datastore does not have support the OR operator, I need to run multiple queries and combine the results. 由于数据存储区不支持OR运算符,因此我需要运行多个查询并合并结果。
Here is my approach (based on the selected answer from my original post): 这是我的方法(基于我原始帖子中选定的答案):
I have achieved #1 by running two separate queries with the async parallel module. 通过使用异步并行模块运行两个单独的查询,我已经获得了第一名。 I need help with step 2. 我需要第2步的帮助。
Question: How to combine (union) two lists of entity keys into a single list (including de-duplication) efficiently? 问题:如何有效地将两个实体密钥列表组合(合并)到单个列表中(包括重复数据删除)?
The code I have below successfully performs both the queries and returns two objects getEntities.requesterEntities and getEntities.dataownerEntities. 我下面的代码成功执行了两个查询,并返回了两个对象getEntities.requesterEntities和getEntities.dataownerEntities。
//Requirement: Get entities for Transfer Requests that match either the Requester OR the Dataowner
async.parallel({
requesterEntities: function(callback) {
getEntitiesByUsername('TransferRequest', 'requester_username', loggedin_username, (treqs_by_requester) => {
//Callback pass in response as parameter
callback(null, treqs_by_requester)
});
},
dataownerEntities: function(callback) {
getEntitiesByUsername('TransferRequest', 'dataowner_username', loggedin_username, (treqs_by_dataowner) => {
//Callback pass in response as parameter
callback(null, treqs_by_dataowner)
});
}
}, function(err, getEntities) {
console.log(getEntities.requesterEntities);
console.log(getEntities.dataownerEntities);
//***HOW TO COMBINE (UNION) BOTH OBJECTS CONTAINING DATASTORE KEYS?***//
});
function getEntitiesByUsername(kind, property_type, loggedin_username, getEntitiesCallback) {
//Create datastore query
const treq_history = datastore.createQuery(kind);
//Set query conditions
treq_history.filter(property_type, loggedin_username);
treq_history.select('__key__');
//Run datastore query
datastore.runQuery(treq_history, function(err, entities) {
if(err) {
console.log('Transfer Request History JSON unable to return data results for Transfer Request. Error message: ', err);
} else {
getEntitiesCallback(entities);
}
});
}
I was able to combine the two separate sets of entity keys by iterating over the arrays and comparing the ID values for each entity key and creating a new array with the unique keys. 通过遍历数组并比较每个实体键的ID值并创建具有唯一键的新数组,我能够组合两组单独的实体键。
Note: The complete solution is posted as an answer to my original post . 注意:完整的解决方案已发布,作为对我原始帖子的答复。
//Union of two arrays of objects entity keys
function unionEntityKeys(arr1, arr2) {
var arr3 = [];
for(var i in arr1) {
var shared = false;
for (var j in arr2)
if (arr2[j][datastore.KEY]['id'] == arr1[i][datastore.KEY]['id']) {
shared = true;
break;
}
if(!shared) {
arr3.push(arr1[i])
}
}
arr3 = arr3.concat(arr2);
return arr3;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.