title: Many to Many with Mongoose
body: <p>I have the two models:</p>
  
  <p><strong>Item.js</strong></p>
  
  <pre><code>const mongoose = require('mongoose');
  
  const itemSchema = new mongoose.Schema({
     name: String,
     stores: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Store' }]
  });
  
  const Item = mongoose.model('Item', itemSchema);
  
  module.exports = Item;
  </code></pre>
  
  <p><strong>Store.js</strong></p>
  
  <pre><code>const mongoose = require('mongoose');
  
  const storeSchema = new mongoose.Schema({
     name: String,
     items: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Item' }]
  });
  
  const Store = mongoose.model('Store', storeSchema);
  
  module.exports = Store;
  </code></pre>
  
  <p>And a <strong>seed.js</strong> file:</p>
  
  <pre><code>const faker = require('faker');
  const Store = require('./models/Store');
  const Item = require('./models/Item');
  
  console.log('Seeding..');
  
  let item = new Item({
     name: faker.name.findName() + " Item"
  });
  
  item.save((err) =&gt; {
     if (err) return;
  
     let store = new Store({
        name: faker.name.findName() + " Store"
     });
     store.items.push(item);
     store.save((err) =&gt; {
        if (err) return;
     })
  });
  </code></pre>
  
  <p>The <code>store</code> is saved with the <code>items</code> array containing 1 <code>item</code>. The <code>item</code> though, doesn't have <code>stores</code>. What am I missing? How to automatically update the many-to-many relationships in MongoDB/Mongoose? I was used to Rails and everything was done automatically.</p>