繁体   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