相关疑难解决方法(0)

在 Mongoose 中使用连接和过滤器进行查询

我是 Mongodb 的新手,并且在我使用 MEAN 堆栈构建的 Web 应用程序中使用它。我的目标是通过连接两个表并对它们应用过滤条件来查询它们。例如:我有两个表 - 自行车-自行车 ID、注册号、品牌、型号和预约 - 预约日期、状态、自行车(参考自行车对象),我只想显示那些没有预约状态=的自行车“已预订”。我想在 Mongoose 中完成以下 SQL。

Select bike.* from Bike inner join Appointment on Bike.BikeID = Appointment.BikeID and Appointment.Status != 'Booked'
Run Code Online (Sandbox Code Playgroud)

我正在使用以下代码,但没有得到所需的结果。有人可以帮我解决这个问题吗?

app.get('/api/getbikeappo*',function(req,res){
        var msg="";
        //.find({cust:req.query._id})
        //.populate('cust','email')
        ubBike.aggregate([
                {
                    $match:
                    {
                        cust : req.query._id
                    }
                },
                {
                    $lookup:
                    {
                        from: "appos",
                        localField: "_id",
                        foreignField: "bike",
                        as : "appointments"
                    }
                },
                {
                    $match:
                    {
                        "appointments" : {$eq : []}
                    }
                }
            ])
            .exec(function(err,bikes){
                 res.send(bikes);
                if(err) throw err;
            });
    }); 
Run Code Online (Sandbox Code Playgroud)
bikes - collection …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb aggregation-framework mean-stack mongodb-aggregation

3
推荐指数
1
解决办法
6402
查看次数