title: How to replace a manual id with an ObjectID _id in mongoDB?
body: <p>Let's say I have a database with two collections, kids and classes. Each kid belongs to one class.</p>
  
  <p>Each class has a previously created integer id. </p>
  
  <p>I want to replace the kid.class_id with the (ObjectID) _id of the class, not the (integer) id of the class.</p>
  
  <p>However, when I run the script below, it doesn't reset the class_id with the class._id -- it remains the old integer id.</p>
  
  <pre><code>  mongoose.connect(someMongodbUri, { useMongoClient: true }, (err, db) =&gt; {
  
      let kidsCount = 0;
  
      db.collection('kids').find({}).each((err, kid) =&gt; {
  
        kidsCount++;
  
        db.collection('classes')
          .findOne({ id: kid.class_id })
          .then((class, err) =&gt; {
  
            let newClassId = class._id;
  
            db.collection('kids').updateOne(
              { _id: kid._id },
              { $set: { class_id: newClassId } }
            ).then(() =&gt; {
                console.info('Updated', kid.class_id);
                kidsCount--;
                if (kidsCount === 0) { db.close(); }
              });
          });
      });
    });
  </code></pre>
  
  <p>Am I missing something? Thanks for any help you can offer!</p>