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

查看:42
本文介绍了找不到使用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天全站免登陆