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
的项目。我很确定这是类型和转换的问题,但我无法弄清楚要做什么。
任何帮助?
答案 0 :(得分:124)
一些提示:
例如:
var ObjectId = require('mongoose').Types.ObjectId;
var query = { campaign_id: new ObjectId(campaign._id) };
答案 1 :(得分:37)
为了改进以前的(正确的)答案,我在我的项目中使用:
String.prototype.toObjectId = function() {
var ObjectId = (require('mongoose').Types.ObjectId);
return new ObjectId(this.toString());
};
// Every String can be casted in ObjectId now
console.log('545f489dea12346454ae793b'.toObjectId());
答案 2 :(得分:8)
不要使用ObjectId通过比较参数来查找,而只需使用
Campaign.findById {req.param('client_id'),function(err,docs)}....
使用objectId查找文档时,findById是最有效的方法...