簡體   English   中英

在mongodb中搜索多個集合

[英]Search multiple collections in mongodb

我有兩個系列,電影和節目。

const ShowsSchema = new Schema({
title: {
 type: String,
 required: true
       },
description: {
 type: String,
 required: true
},
image: {
 type: String,
 required: true
})

目前我只能從一個集合中搜索數據,但我想從兩者中獲取數據。 在瀏覽網站上的一些解決方案后,我遇到了$ lookup字段。

 router.get('/:query', (req, res, next) => {
  const query = req.params.query;
  Show.aggregate([{

   $lookup: {
    from: "Movies",
    localField: "title",
    foreignField: "title",
    as: "movies"
     }
  }], () => {

  Show.find({
  title: {
    $regex: new RegExp(query),
    $options: "$i"
  },

}, (err, shows) => {
  if (err) {
    console.error(err);
  }
  res.json(shows);
  });
 });
});

我被困在這里任何幫助將不勝感激。

router.get('/:query', (req, res, next) => {
   const query = req.params.query;
   Show.aggregate([{
       $match : {
        title: {
            $regex: new RegExp(query),
            $options: "$i"
        }
      }
   },{
      $lookup: {
         from: "Movies",
         localField: "title",
         foreignField: "title",
         as: "movies"
      }
   }],(err, shows) => {
     if (err) {
         console.error(err);
     }
     res.json(shows);
   });
 });

希望這是有幫助的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM