gpt4 book ai didi

regex - 如何在 Mongoose 中使用正则表达式查找项目

转载 作者:IT老高 更新时间:2023-10-28 13:14:42 25 4
gpt4 key购买 nike

在 Mongoose 文档中,我没有找到 $regex 的等效项MongoDb 的。你能提供一个带有正则表达式的简单 Mongoose find() 吗?

最佳答案

Mongoose doc for find .

mongodb doc for regex .

   var Person = mongoose.model('Person', yourSchema);
// find each person with a name contains 'Ghost'
Person.findOne({ "name" : { $regex: /Ghost/, $options: 'i' } },
function (err, person) {
if (err) return handleError(err);
console.log('%s %s is a %s.', person.name.first, person.name.last, person.occupation);

});

注意我们传递给 mongoose.findOne 函数的第一个参数。 "{ "name": { $regex:/Ghost/, $options: 'i' } }"。 “名称”是您正在搜索的文档的字段。 “鬼”是正则表达式。 "i"用于不区分大小写的匹配。希望这会对你有所帮助。

关于regex - 如何在 Mongoose 中使用正则表达式查找项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38497650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com