Py学习  »  MongoDB

mongodb find查询:查找带空格的字段

Ashy Ashcsi • 4 年前 • 941 次点击  

集合中的mongodb记录示例如下:

[
{
_id: "5ae57dba117af6dfc1e49c15",
BANK: "CITI BANK",
IFSC: "CITI0000005",
MICR: 411037001,
BRANCH: "PUNE",
ADDRESS: "2413, KUMAR CAPITAL, EAST STREET, PUNE 411 001",
CONTACT: 5606356,
CITY: "PUNE",
DISTRICT: "PUNE",
STATE: "MAHARASHTRA"
}
]

我正在寻找与“花旗银行”、“花旗银行”、“花旗”匹配的记录。以下查询适用于“花旗”或“花旗银行”,但不适用于“花旗银行”。请在下面查找我的查询:

try {

    let orCond = [];
    let arrBranches = req.params.branch.split(" ");

    arrBranches.map((branch) => {
        orCond.push({ BRANCH: { $regex: '.*' + branch + '.*', $options: 'i' } });
    });

    console.log(orCond);

    const banks = await Bank.find(
        {
            $and: [
                {
                    BANK: { $regex: '.*' + req.params.bank + '.*', $options: 'i' }
                },
                {
                    $or: orCond
                }
            ]
        }
    ).lean().limit(1);

    console.log(banks);

    res.status(200).send(banks);
} catch (error) {
    console.log('error', error);
}

什么查询将检索其中包含空格的字段?

谢谢

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/50962
 
941 次点击