find by _id with Mongoose
<p>I am having trouble with a simple findById with mongoose.</p>

<p>Confirmed the item exists in the DB</p>

<pre><code>db.getCollection('stories').find({_id:'572f16439c0d3ffe0bc084a4'})
</code></pre>

<p>With mongoose</p>

<pre><code>  Story.findById(topic.storyId, function(err, res) {
    logger.info("res", res);
    assert.isNotNull(res);
  });
</code></pre>

<p>won't find it.</p>

<p>I also tried converting to a mongoId, still cannot be found (even though mongoose supposedly does this for you)</p>

<pre><code>var mid = mongoose.Types.ObjectId(storyId);
let story = await Story.findOne({_id: mid}).exec();
</code></pre>

<p>I'm actually trying to use this with typescript, hence the await.</p>

<p>I also tried the <code>Story.findById(id)</code> method, still cannot be found.</p>

<p>Is there some gotcha to just finding items by a plain <code>_id</code> field?
does the _id have to be in the Schema? (docs say no)</p>

<p>I can find by other values in the Schema, just <code>_id</code> can't be used...</p>

<hr>

<p>update: I wrote a short test for this.</p>

<pre><code>describe("StoryConvert", function() {


  it("should read a list of topics", async function test() {
    let topics = await Topic.find({});

    for (let i = 0; i &lt; topics.length; i ++) {
      let topic = topics[i];
    // topics.forEach( async function(topic) {
      let storyId = topic.storyId;
      let mid = mongoose.Types.ObjectId(storyId);
      let story = await Story.findOne({_id: mid});
      // let story = await Story.findById(topic.storyId).exec();
      // assert.equal(topic.storyId, story._id);
      logger.info("storyId", storyId);
      logger.info("mid", mid);
      logger.info("story", story);
      Story.findOne({_id: storyId}, function(err, res) {
        if (err) {
          logger.error(err);
        } else {
          logger.info("no error");
        }
        logger.info("res1", res);
      });

      Story.findOne({_id: mid}, function(err, res) {
        logger.info("res2", res);
      });

      Story.findById(mid, function(err, res) {
        logger.info("res3", res);
        // assert.isNotNull(res);
      });

    }

  });


});
</code></pre>

<p>It will return stuff like</p>

<pre><code>Testing storyId 572f16439c0d3ffe0bc084a4

Testing mid 572f16439c0d3ffe0bc084a4

Testing story null

Testing no error

Testing res1 null

Testing res2 null

Testing res3 null
</code></pre>

<p>I noticed that <code>topic.storyId</code> is a string
not sure if that would cause any issues mapping to the other table.
I tried also adding some type defs</p>

<pre><code>  storyId: {
    type: mongoose.Schema.Types.ObjectId,
    required: false
  }
</code></pre>

<p><a href="https://i.stack.imgur.com/ZluLs.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ZluLs.png" alt="enter image description here"></a></p>