gpt4 book ai didi

node.js - Mongoose find().or 挂起

转载 作者:太空宇宙 更新时间:2023-11-03 23:15:09 25 4
gpt4 key购买 nike

mongodb 新手。我正在尝试进行多字段搜索查询。当我运行以下代码时,它会根据关键字正确返回结果。

var re = new RegExp(req.query.keyword, 'i');
Company.find({ 'name': { $regex: re }},
function (err, company) {
if (err) return next(err);
res.send(company);
})

但是,当我尝试使用 find().or 使用此代码时,我的 API 只是挂起然后超时,没有错误。

var re = new RegExp(req.query.keyword, 'i');
Company.find().or([{ 'name': { $regex: re }}, { 'url': { $regex: re }}],
function (err, company) {
if (err) return next(err);
res.send(company);
})

有什么想法吗?谢谢!

最佳答案

Query.or不接受回调函数作为参数,所以必须调用exec执行查询

Company.find()
.or([{ 'name': { $regex: re }}, { 'url': { $regex: re }}])
.exec(function (err, company) {
if (err) return next(err);
res.send(company);
})

关于node.js - Mongoose find().or 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56415746/

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