使用 Mongoose 找不到按 ObjectId 搜索的文档 [英] Can't find documents searching by ObjectId using Mongoose

查看:40
本文介绍了使用 Mongoose 找不到按 ObjectId 搜索的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Campaign.find {client_id:req.param('client_id')}, (error, campaigns) ->
    if error
      response =
        error: error.message
    else
      for campaign in campaigns
        query =
          campaign_id: campaign._id
        console.log query
        CampaignResponse.find query, (err, campaignResponsesCount) ->
          console.log campaignResponsesCount

      response = campaigns

    res.json response

出于某种原因,这会返回结果.但是,CampaignResponse 中有具有该特定 campaign._id 的项目.我很确定这是类型和转换的问题,但我不知道该怎么做.

For some reason, this returns no results. However, there are items in CampaignResponse with that specific campaign._id. I'm pretty sure this is an issue with types and casting, but I can't figure out what to do.

有什么帮助吗?

推荐答案

一些提示:

  • 尝试在命令行中从 mongodb 运行相同的查询,看看是否有任何结果.
  • campaign_id"是否在您的架构中定义为 ObjectId?如果是这样,请尝试使用 ObjectId 类型进行搜索.

例如:

var ObjectId = require('mongoose').Types.ObjectId; 
var query = { campaign_id: new ObjectId(campaign._id) };

这篇关于使用 Mongoose 找不到按 ObjectId 搜索的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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