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

标签 javascript mongodb mongodb-stitch

我是 MongoDB 新手,所以这可能是一个基本问题,但我在如何解决它方面遇到了困难。

我想在我的收藏中找到一份文档,如下所示:

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

返回的字段之一是包含其他文档的“curid”(唯一值)的数组:

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

我想要做的是找到每个其他文档并将它们附加到已经存在的查找查询结果中。我确信一定有一种方法无需对数组中的每个元素进行新的搜索。

(如果有什么不同的话,我正在使用 mongoDB Atlas 的 Stitch)

最佳答案

如果我正确理解了问题,您想使用 HDP_Topics 字段来触发新的搜索。您可以使用 $graphLookup为此:

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

如果您想要更多递归级别,请更改 maxDepth。此搜索会将结果文档放在 otherDocs 下。

关于javascript - MongoDB 检索另一个文档数组中包含的每个文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52543139/

相关文章:

mongodb - 聚合: Perform countBy on multiple $lookup arrays in a single query

ruby-on-rails - rails中的原始mongodb查询

node.js - Node js Monk 更新持续存在

MongoDB Stitch 返回数据作为 $NumberDouble 而不是数字本身

mongodb - 在 Mongo-Atlas Stitch 中使用上下文时出错

javascript - 两个类之间的回调如何工作?

javascript - 使用文件名从本地目录加载多个图像

javascript - 如何检查 javascript 对象中的值?

javascript - 如何使用 jQuery 从 Google 读取联系人信息?

amazon-s3 - 如何使用 mongoDB Stitch 获取 AWS S3 签名 URL?