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
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.
Any help?
ObjectId . A SchemaType is just a configuration object for Mongoose. An instance of the mongoose. ObjectId SchemaType doesn't actually create MongoDB ObjectIds, it is just a configuration for a path in a schema.
Mongoose | findById() Function The findById() function is used to find a single document by its _id field. The _id field is cast based on the Schema before sending the command.
MongoDB provides a function with the name findById() which is used to retrieve the document matching the 'id' as specified by the user. In order to use findById in MongoDB, find() function is used. If no document is found matching the specified 'id', it returns null.
The find() function is used to find particular data from the MongoDB database. It takes 3 arguments and they are query (also known as a condition), query projection (used for mentioning which fields to include or exclude from the query), and the last argument is the general query options (like limit, skip, etc).
A couple tips:
For example:
var ObjectId = require('mongoose').Types.ObjectId; var query = { campaign_id: new ObjectId(campaign._id) };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With