【问题标题】:filtering documents using multi properties (refrenced and nrml) in mongoose and express using request query在 mongoose 中使用多属性(refrenced 和 nrml)过滤文档,并使用请求查询表达
【发布时间】:2020-05-18 17:00:43
【问题描述】:

所以我想获得在特定日期和按价格(最小、最大)和地点(wilaya、daira 和 commune)可用的销售,所以预订是销售参考,包含日期属性和联系人包含销售地点。

const contacteSchema = new mongoose.schema({
    telephone:[{
            type:Number,
            minlength:10,
            maxlength:10,
            required:true
    }],
    adress:{
        wilaya:{
            type:String,
            minlength:5,
            maxlength:20,
            required:true
        },
        daira:{
            type:String,
            minlength:5,
            maxlength:50,
            required:true
        },
        commune:{
            type:String,
            minlength:5,
            maxlength:50,
            required:true
        },
        rue:{
            type:String,
            minlength:5,
            maxlength:50,
            required:true
        }
    }

});

const Contacte = mongoose.model('Contacte',contacteSchema)

这是预订模式

const bookingSchema = new mongoose.schema({
    date:{
        type: Date,
        minlength:10,
        maxlength:10,
        required:true,
    },
    total:{
        type:Number,
        minlength:5,
        maxlength:50,
        required:true
    },
    confirmationsale:{
        type:Boolean,
        default:false
    },
    client:{
        type: mongoose.Schema.Types.ObjectId,
        ref:'Client'
    },
    sale:{
        type: mongoose.Schema.Types.ObjectId,
        required:true,
        ref:'Sale'
    },
    services:[{
        type: mongoose.Schema.Types.ObjectId,
        ref:'SaleService'
    }]
});

const Booking = mongoose.model('Booking',bookingSchema)

这是销售模式

var saleSchema = new mongoose.Schema({
    name:{
        type:String,
        minlength:5,
        maxlength:50,
        required:true
    },
    description:{
        type:String,
        minlength:5,
        maxlength:255,
    },
    rating:{
        type:Number,
        minlength:1,
        maxlength:255,
        default:0,
    },
    price:{
        type:Number,
        minlength:5,
        maxlength:50,
        required:true
    },
    login:{
        type:mongoose.Schema.Types.ObjectId,
        ref:'Login'
    },
    contacte:{
        type:mongoose.Schema.Types.ObjectId,
        ref:'Contacte'
    },
    services:[{
        type:mongoose.Schema.Types.ObjectId,
        ref:'SaleService'
    }],
    pictures:[{
        type:mongoose.Schema.Types.ObjectId,
        ref:'Picture'
    }],
    bookins:[{
        type:mongoose.Schema.Types.ObjectId,
        ref:'Booking'
    }]
});

var Sale = mongoose.model('Sale',saleSchema);

这是用于搜索和过滤销售的 router.get 方法

router.get('/get/sale', async (req, res)=>{

    let skip = parseInt(req.query.offset);
    let nPerPage = parseInt(req.query.limit);
    let sortby = req.query.sort;
    let orderby = req.query.order;
    let date = new Date(req.query.date);
    let wilaya = req.query.wilaya;
    let daira = req.query.daira;
    let commune = req.query.commune;
    let pricemax = req.query.pricemax;
    let pricemin = req.query.pricemin;

    let salemodel = await Sale.find({
        $or:[{
                bookins: {
                    $in : [{
                        'booking.date' : {
                             $ne: date
                        },
                    }]
                }
            },
            {
                $and:[
                    {'contacte.wilaya':wilaya},
                    {'contacte.daira':daira},
                    {'contacte.commune':commune}
                ]
            },
            {
                $and:[
                    {
                        'price': {
                            $lte: pricemax
                        }
                    },
                    {
                        'price': {
                            $gte: pricemin
                        }
                    }
                ]
            }
        ]
    })
    .populate('login email picture -password -username -type')
    .populate('contacte')
    .sort([[sortby, orderby]])
    .skip(skip > 0 ? (skip+1) : 0).limit(nPerPage)
    .exec();
    if(!salemodel) return res.status(404).send('invalid sale');
});

这是正确的还是我必须修改一些代码或必须摆脱它?

【问题讨论】:

    标签: node.js mongodb express mongoose


    【解决方案1】:

    所以它不会起作用,因为我想要的是基于查询的过滤搜索,在这里我们必须提供 picemin 和 max 以及 wilaya 和 daira 和公社来获得它,所以我正在构建一个基于查询的过滤器,例如,如果我只有最小和最大价格,我将通过使用对象并添加过滤器来为其添加过滤器,并且为其他参数添加相同的过滤器。

    【讨论】:

      猜你喜欢
      • 2015-03-23
      • 2021-07-07
      • 1970-01-01
      • 2012-01-15
      • 2021-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      相关资源
      最近更新 更多