简体   繁体   English

NodeJ:使用另一个集合的结果过滤mongo查询结果

[英]NodeJs: Filter mongo query results with results from another collection

I have a situation where I need to perform logic on a distinct set of values from a mongo collection (A) and then save result to another collection (B). 我遇到一种情况,需要对mongo集合(A)的一组不同值执行逻辑,然后将结果保存到另一个集合(B)。 However the contents of (A) will change over time and so I only want to perform the logic on those documents in (A) where there is not a corresponding document in (B). 但是(A)的内容会随着时间而变化,因此我只想对(B)中没有相应文档的(A)中的那些文档执行逻辑。 As joins aren't supported, I am trying to do this at the Node level. 由于不支持联接,因此我尝试在节点级别执行此操作。 I am querying all items in collection (A) and using findOne to look for the corresponding entry in collection (B). 我正在查询集合(A)中的所有项目,并使用findOne在集合(B)中查找相应的条目。 If I find it, I would like to remove it from the array, but I am stuck because findOne uses an asynchronous callback which doesn't seem to work with the array filter method. 如果找到它,我想将其从数组中删除,但是我被卡住了,因为findOne使用了异步回调,该回调似乎不适用于数组filter方法。 Is there a better way to do this: 有一个更好的方法吗:

function loadNewDocumentsFromDB(callback){
  db.collection('A', function(err, acollection){    
    bcollection.find().toArray(function(err, arr){
      if(arr){
        // toQuery is the global array to be used elsewhere
        toQuery = arr.map(function(config){
          transformed = 
          {
            args: config._id, // these args are a doc representing a unique entry in 'B'
            listings: config.value.id.split(',') // used by other functions
          };

          return transformed;
        })
        .filter(function(transformed){
          db.collection('B', function(err, bcollection){
          bcollection.findOne(transformed.args, function(err, doc){
            // I want these values returned from the filter function not the callback
            if(doc){
              return false; // want to remove this from list of toQuery
            }else{
              return true; // want to keep in my list
          });
        });
      }
      callback();
    });
  }); 
}

This was how I managed to get it working: 这就是我设法使其工作的方式:

function loadOptionsFromDB(callback){
  toQuery = [];
  db.collection('list', function(err, list){    
    db.collection('data', function(err, data){
      list.find().each(function(err, doc){
        if(doc){ 
          transformed = 
            {
              args: doc._id,
              listings: doc.value.id.split(',')
            };
          (function(obj){       
            data.findOne(obj.args, function(err, found){
              if(found){}
              else{
                toQuery.push(obj);
              }
            });
          })(transformed);
        }else{
      //Done finding
          setTimeout(callback, 20000);
        }
      });       
    });
  }); 
}

A better way would be to do this on the database. 更好的方法是在数据库上执行此操作。 Check if 2 executions of http://docs.mongodb.org/manual/core/map-reduce/ would be of any use to you. 检查是否有两次执行http://docs.mongodb.org/manual/core/map-reduce/对您有用。 See Merging two collections in MongoDB for more information 有关更多信息,请参见在MongoDB中合并两个集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM