title: Mongoose, find by element in array
body: <p>I'm trying to find all documents with a certain value in an array field using Mongoose and Node.js. I can do this in MongoDB with no trouble, but I'm having difficulty in Mongoose. I used <a href="https://stackoverflow.com/questions/18148166/find-document-with-array-that-contains-a-specific-value">Find document with array that contains a specific value</a> as my guide for how to do this, but I'm not getting the results I expect.
  My model:</p>
  
  <pre><code>const mongoose = require("mongoose");
  
  const Schema = mongoose.Schema;
  const ApptSchema = new Schema({
    store: { type: String, required: true },
    dotw: { type: String, required: true },
    month: { type: Number, required: true },
    day: { type: Number, required: true },
    hr: { type: Number, required: true },
    min: { type: Number, required: true },
    customers: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }],
    full: { type: Boolean, required: true, default: false }
  });
  
  const Appt = mongoose.model("Appt", ApptSchema);
  
  module.exports = Appt;
  </code></pre>
  
  <p>I want to find all documents that contain a certain customer id. In MongoDB shell, this is what I would do:</p>
  
  <pre><code>db.appts.find({customers: "5e7e3bc4ac4f196474d8bf69"})
  </code></pre>
  
  <p>This works as expected, giving me all documents, (one document in this case), where this id is in the <code>customers</code> array.</p>
  
  <pre><code>{ "_id" : ObjectId("5e7e719c806ef76b35b4fd69"), "customers" : [ "5e7e3bc4ac4f196474d8bf69" ], "full" : false, "store" : "Nashville", "dotw" : "Friday", "month" : 4, "day" : 15, "hr" : 13, "min" : 0 }
  </code></pre>
  
  <p>In Mongoose, this is what I'm trying:</p>
  
  <pre><code>Appt.find({ customers: "5e7e3bc4ac4f196474d8bf69" }, (err, docs) =&gt; {
      if (err) {
        console.log(err);
      } else {
        console.log(docs);
      }
    });
  </code></pre>
  
  <p>This prints an empty array, even though there is clearly a document where this id is in the <code>customers</code> array.
  This seems like this should work, but I'm clearly missing some piece of the puzzle. Any insight into what I'm doing wrong would be very much appreciated.</p>
  
  <p>Edit: In case anyone would like to/be willing to take a more in-depth look, a GitHub repo of the app so far can be found <a href="https://github.com/LuosRestil/apptSolution" rel="nofollow noreferrer">here</a>. The query in question is in routes/routes.js at line 111 (as of the time of writing).</p>
  
  <p>Another edit: It appears this has something to do with the Schema type of the field in question. I eliminated the <code>ref</code> attribute of the entries in the <code>customers</code> field, just in case that was causing a problem, but my query still returned an empty array. The next test was to add a new field to my model, <code>myStrings: [String]</code>. I then added a string to the array of one of my Appt documents, <code>"working"</code>, and queried <code>Appt.find({myStrings: "working"})</code> and this finally returns the Appt document that I updated. This tells me there's something squirrely about working with the <code>mongoose.Schema.Types.ObjectId</code>, but I can't figure out how to resolve it.</p>
  
  <p>FINAL EDIT: After much tribulation, this is solved. The issue was as follows...
  For testing, I was adding items to my database using MongoDB shell, which does not enforce data types like Mongoose does. I didn't realize that I was simply adding user ids as strings to the <code>customers</code> array. When Mongoose went looking for ObjectIds, of course it didn't find any, and returned an empty array. Adding customers to the <code>customers</code> array with <code>db.appts.updateOne({&lt;whatever information&gt;},{$push:{customers: new ObjectId(&lt;id string&gt;)}})</code>, Mongoose was able to return the information I was looking for.</p>