繁体   English   中英

Mongodb的多个条件

[英]Mongodb multiple conditions

MongoDB的新功能,可以表达互动。 我正在尝试根据URL参数查询数据库,本质上,我想获取URL参数,然后将它们添加到对象中(如果它们存在于查询数据库时使用)。 我设置了我的表达路径,并且了解到find()方法接受一个对象,当我像下面这样对键值对进行硬编码时,我会获得正确的数据,但是当我设置一个称为filter的对象时,它具有与硬编码的示例和取消注释的过滤器以及注释状态和类型,然后将其传递给find方法,我得到一个空数组。 原因是什么?实现此目标的最佳方法是什么?

    app.get('/query', function (req, res) {

    var filter = {

        state: "florida",
        type: "red"
    }

    db.collection('tickets').find({ 
            //filter
            state:"florida",
            type:"red"

        })
        .toArray(function (err, documents) {
            // Here is where we decide what to do with the query results
            if (err) 
                throw err
            res.send(documents)

        })
});

find方法接受一个查询对象作为第一个参数,该查询对象就是您自己的filter对象。 因此,您应该这样做:

db.collection('tickets').find(filter)
            .toArray(function (err, documents) {
                // Here is where we decide what to do with the query results
                if (err) 
                    throw err
                res.send(documents)

            })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM