我已经将节点路径构造为/57639afa5961debc1b256745/, /5763c5d17d05688c1838c7a3/,
,但是当我尝试用nodepath
替代时,即使它具有子代,它也会将结果作为空数组获取:
Degree.prototype.findByPath = function(dbObj, degrees) {
var nodePath = '';
for(var i in degrees) {
nodePath = nodePath + '/' + degrees[i]['_id'] + '/, ';
}
return new Promise(function (resolve, reject) {
console.log(nodePath);
dbObj.collection('degree').find({
'path': {
'$in':
[ nodePath ]
}
}).toArray((err, results) => {
if(err) {
reject(err);
} else {
var nodes;
if(results != null)
nodes = degrees.concat(results);
else
nodes = degrees;
resolve(nodes);
}
});
});
};
最佳答案
您需要将nodePath
构建为字符串数组,而不是一个逗号分隔的字符串。而且,如果要将id值视为正则表达式,请使用RegExp
构造函数,而不要添加斜杠字符:
var nodePath = [];
for(var i in degrees) {
nodePath.push(new RegExp(degrees[i]['_id']));
}
return new Promise(function (resolve, reject) {
console.log(nodePath);
dbObj.collection('degree').find({
'path': {
'$in': nodePath
}
}).toArray((err, results) => {...