提问者:小点点

如何将mysql查询转换为mongodb查询?


我使用了cakephp Mysql到mongodb查询组件,即将Mysql查询转换到mongodb中,但是当查询有多个括号时代码停止工作,我还尝试将http://www.querymongo.com/site上的查询转换为相同的问题,

app.get("/products", function (req, res) {
    const marche_id = req.query.marche_id;
    con.query(
        // `SELECT DISTINCT libelle, code_libelle, marche FROM marche_all WHERE ${
        `SELECT DISTINCT libelle, code_libelle, marche FROM marche_alpha WHERE ${marche_id !== undefined ? `(marche = '${marche_id}')` : ""
        } `,
        function (err, data) {
            err ? res.send(err) : res.json(data);
        }
    );
});

app.get("/product", function (req, res) {
    const product_id = req.query.product_id;
    const marche_id = req.query.marche_id;

    con.query(
        // `SELECT * FROM marche_all WHERE (marche = '${marche_id}')
        `SELECT * FROM marche_alpha WHERE (marche = '${marche_id}')
    AND (code_libelle=${product_id})
`,
        function (err, data) {
            err ? res.send(err) : res.json(data);
        }
    );
});

共1个答案

匿名用户

//sql
`SELECT DISTINCT libelle, code_libelle, marche FROM marche_alpha WHERE ${marche_id !== undefined ? `(marche = '${marche_id}')` : "";
//mongo
db.marche_alpha.find({ marche: marche_id}, {libelle: 1, code_libelle: 1});
//sql
`SELECT * FROM marche_alpha WHERE (marche = '${marche_id}') AND (code_libelle=${product_id})`;
//mongo
db.marche.findOne({$and [{marche: marche_id}, {code_libelle: product_id}}];