【问题标题】:How can I query multiple collections in mongodb at the same time?如何同时查询 mongodb 中的多个集合?
【发布时间】:2021-07-29 21:29:25
【问题描述】:
books
{
  name: "random",
  stars: 4,
},
{
  name: "random",
  stars: 2,
}

foods
{
  name: "random",
  stars: ,
},
{
  name: "random",
}

movies
{
  name: "random",
  stars: 0,
},
{
  name: "random",
  stars: 7,
}

在我的名为 dbcosos 的数据库中获得了这个集合,我想获得超过 3 星的项目 有人可以帮帮我吗?

【问题讨论】:

  • 不能同时查询多个集合

标签: node.js mongodb mongoose collections


【解决方案1】:

您无法在单个查询中从多个集合中获取数据。相反,您可以在这种情况下使用find() 来获得所需的结果。

let bookData = await booksSchema.find({$and:[{stars:{$exsits: true}},{stars:{$gte:3}}]})

foodsmovie 集合也做同样的事情。并且使用$exists 运算符,因为它看起来stars 是一个可选字段。

【讨论】:

    猜你喜欢
    • 2019-05-09
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 2020-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多