node.js Moongodb 两个发现 [英] node.js moongodb two finds

查看:65
本文介绍了node.js Moongodb 两个发现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上次纠结于如何在 nodejs 中放置两个 find 参数:这是我当前的查询:

I struggle in last time how to put two find paraments in nodejs: This is my currently query:

userWithCompanies = await User.findById(userId).populate('companies');

但接下来我需要在其他部分制作搜索系统,例如:

But in the next i need to make search system here in other sections working search query is for example:

companies = await Company.find({title: {$regex: `.*${search}.*`, $options: "i"}});

所以我尝试了这样的事情:

So i tried something like this:

await User.findById(userId).populate('companies').find({title: {$regex: `.*${search}.*`, $options: "i"}});

但我刚刚收到错误:

UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)

推荐答案

您需要提供 查询条件用于填充方法.
像这样:

You need to provide query condition for populate method.
Something like this:

const userWithCompanies = await User
  .findById(userId)
  .populate({
    path: 'companies',
    match: {
      title: {$regex: `.*${search}.*`, $options: "i"}
  });

这篇关于node.js Moongodb 两个发现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆