title: Mongoose: CastError: Cast to ObjectId failed for value "[object Object]" at path "_id"
body: <p>I am new to node.js, so I have a feeling that this will be something silly that I have overlooked, but I haven't been able to find an answer that fixes my problem. What I'm trying to do is create a path that will create a new child object, add it to the parent's array of children, then return the child object to the requester. The problem that I am running into is that if I pass the string id into findById, node crashes with</p>
  
  <blockquote>
    <p>TypeError: Object {} has no method 'cast'</p>
  </blockquote>
  
  <p>If I try to pass in an ObjectId instead, I get</p>
  
  <blockquote>
    <p>CastError: Cast to ObjectId failed for value "[object Object]" at path "_id"</p>
  </blockquote>
  
  <p>Here is a rough outline of my code:</p>
  
  <pre><code>var mongoose = require('mongoose');
  var Schema = mongoose.Schema;
  var ObjectId = Schema.ObjectId; //Have also tried Schema.Types.ObjectId, mongoose.ObjectId
  
  mongoose.connect('mongodb://user:password@server:port/database');
  
  app.get('/myClass/:Id/childClass/create', function(request, result) {
    var id = new ObjectId(request.params.Id);
    MyClass.findById(id).exec( function(err, myClass) {
      if (err || !myClass) { result.send("error: " + err + "&lt;br&gt;" + JSON.stringify(id) || ("object '" + request.params.Id + "' not found: " + id)); return; }
      var child = ChildClass();
      myClass.Children.addToSet(child);
      myClass.save();
      result.send(child);
    });
  });
  </code></pre>
  
  <p>If I execute this code with the path "/myClass/51c35e5ced18cb901d000001/childClass/create", this is the output of the code:</p>
  
  <blockquote>
    <p>error: CastError: Cast to ObjectId failed for value "[object Object]" at path "_id"
    {"path":"51c35e5ced18cb901d000001","instance":"ObjectID","validators":[],"setters":[],"getters":[],"_index":null}</p>
  </blockquote>
  
  <p>I've tried using findOne and passing in {_id:id} instead, but this appears to be exactly what findById does. I've tried the different classes for ObjectId that I've seen listed on other sites. I've tried calling ObjectId() like a function instead of a constructor and that returns undefined. At this point, I'm running out of ideas and it doesn't seem that googling for an answer is helping. Any ideas on what I'm doing wrong?</p>
  
  <p>Also, like I said, I'm new to node/Mongo/Mongoose/Express, so if there is a better way to accomplish my goal, please let me know. I appreciate all feedback.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>After the workaround from Peter Lyons, I googled another error that I was running into and found findByIdAndUpdate, which works as expected and does exactly what I was hoping to do. I'm still not sure why findById and findOne were giving me such issues and I'm curious to know (maybe a bug report needs to be filed), so I'll leave this open in case someone else has an answer.</p>