简体   繁体   English

MongoDB检索包含在另一个文档数组中的每个文档

[英]MongoDB retrieve every document contained in another document's array

I'm new to MongoDB so this might be a basic question but I am having trouble in how to approach it. 我是MongoDB的新手,所以这可能是一个基本问题,但是我在处理方法上遇到了麻烦。

I want to find one document in my collection, like so: 我想在集合中找到一个文档,如下所示:

db.collection("articles").find({curid:"1000143"}).asArray();

One of the fields returned is an array containing other document's 'curid' (a unique value): 返回的字段之一是包含其他文档的“ curid”(唯一值)的数组:

HDP_Topics = ["1124516", "101388", "1031462", "1053284", "1077080", "1150760", "1092377", "1100194", "1103692", "1135134", "1134909", "1119820", "1000634", "1120316", "1000143"]

What I want to do is to find each of these other documents and append them onto the already existing find query results. 我要做的是查找其他所有文档,并将它们附加到已经存在的查找查询结果中。 I'm sure there must be a way without having make a new search for every element in the array. 我确定必须有一种方法,不必对数组中的每个元素进行新的搜索。

(I'm using mongoDB Atlas's Stitch if that makes any difference) (如果有任何区别,我使用的是mongoDB Atlas的Stitch)

If I understand the question correctly, you want to use the HDP_Topics field to trigger a new search. 如果我对问题的理解正确,那么您想使用HDP_Topics字段触发新的搜索。 You can use a $graphLookup for this: 您可以为此使用$ graphLookup

db.articles.aggregate( [
   {
      $graphLookup: {
         from: "articles",
         startWith: "1000143",
         connectFromField: "HDP_Topics",
         connectToField: "curid",
         as: "otherDocs",
         maxDepth: 2
      }
   }
] )

If you want to have more recursion level, change maxDepth . 如果您想要更多的递归级别,请更改maxDepth This search will put the resulting documents under otherDocs . 此搜索会将结果文档放在otherDocs下。

It ain't pretty, but I couldn't see any other alternative. 它不是很漂亮,但是我看不到其他选择。 So this is what I did in the end for a quick fix. 因此,这就是我最终为快速修复所做的事情。 Hopefully the aggregation functions (thanks @enys) will be implemented soon. 希望聚合功能(感谢@enys)将很快实现。

function getDataByTitle(title){
    try{
      var deferred = $q.defer();
      var docs = db.collection("articles").find({title:title}).asArray().then(
        docs => {
          var newData = [[docs[0]]];
          var docsLength = $rootScope.articlesToCompare;
          for(let i=1, p = Promise.resolve(); i < docsLength; i++){
            p = p.then(_ => new Promise(resolve => {
                newData.push(db.collection("articles").find({curid:docs[0].cosineArray[i]}).asArray());
                resolve();
                if(i==docsLength-1) deferred.resolve(newData);
            }));
          }
        }
      );
      return deferred.promise;
    } catch(err) {
      console.log(err);
    }
 }

Note: I changed from 'HDP_Topics' to 'cosineArray'. 注意:我从“ HDP_Topics”更改为“ cosineArray”。 Also docsLength is predefined, which just works as a $limit alternative, seeing as I'm using multiple queries I can't defined this in the query itself. docsLength也是预定义的,它只能用作$ limit的替代品,因为我正在使用多个查询,因此无法在查询本身中定义它。

The result is an array of arrays of articles. 结果是一组文章数组。 You can see it in action here: unitroll.me , using the search bar on the top-right. 您可以在右上角的搜索栏中查看它的运行情况: unitroll.me

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

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