English 中文(简体)
查阅文件 使用Mongoose
原标题:Can t find documents searching by ObjectId using Mongoose
  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

由于某种原因,这一回报no结果。 但是,<代码>CampaignResponse中有具体项目campaign._id。 我确信,这是一个涉及类型和投放的问题,但我可以指出需要做些什么。

任何帮助?

最佳回答

两端:

  • Try running the same query from mongodb at the command line, see if you get any results.
  • Is the "campaign_id" defined as an ObjectId in your schema? If so, try searching using the ObjectId type.

例如:

var ObjectId = require( mongoose ).Types.ObjectId; 
var query = { campaign_id: new ObjectId(campaign._id) };
问题回答

仅改进先前(更正)的答复,即利用我的项目:

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());

不要用目标来比较你的参数,而只是使用

Campaign.findById{req.param(客户_id ),Function(err,docs)}....

当用物体找到docs时 Id a Id是最有效的方法......





相关问题
Access DB Ref MongoDB

Whats the best way to access/query a DB Ref: UPDATE: users: name, groupref : {$ref:"groups",$id:"ObjectId ..." } } groups: name, topic, country,...,.. Assumption is that user belongs to only one ...

MongoDB nested sets

What re the best practices to store nested sets (like trees of comments) in MongoDB? I mean, every comment can have a parent comment and children-comments (answers). Storing them like this: { ...

MongoMapper and migrations

I m building a Rails application using MongoDB as the back-end and MongoMapper as the ORM tool. Suppose in version 1, I define the following model: class SomeModel include MongoMapper::Document ...

MongoDB takes long for indexing

I have the following setup: Mac Pro with 2 GB of RAM (yes, not that much) MongoDB 1.1.3 64-bit 8 million entries in a single collection index for one field (integer) wanted Calling .ensureIndex(...) ...

Storing and accessing large amounts of data

My application creates pieces of data that, in xml, would look like this: <resource url="someurl"> <term> <name>somename</name> <frequency>somenumber</...

热门标签