简体   繁体   中英

How can I query multiple collections in mongodb at the same time?

books
{
  name: "random",
  stars: 4,
},
{
  name: "random",
  stars: 2,
}

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

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

got this collections in my database called dbcosos and I wanna get the items who had more than 3 stars could someone give me a hand?

You can't fetch data from multiple collections in a single query. Instead u can use find() in this case to get the required result.

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

Do same thing for foods and movie collection as well.And $exists operator is used since it looks like stars is an optional field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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