MongoDB: Conditional select from one collection based on another collection

后端 未结 2 1880
广开言路
广开言路 2021-01-29 02:22

I\'m fairly new to MongoDB and need help doing a select, or perhaps some sort of left join, on one collection based on another collection\'s data.

I have two collections

2条回答
  •  温柔的废话
    2021-01-29 02:50

    MongoDB supports joins with $lookup , In your case you can use query like:-

        db.animals.aggregate([
        {
          $lookup:
            {
              from: "meals",
              localField: "lastMeal",
              foreignField: "id",
              as: "last_meal"
            }
       },
      {
       $match: { 
              "created" : {
                    $gt: "date" //your date format
              }
           } 
     }
    ])
    

    thanks !

提交回复
热议问题