title: Mongoose One to Many relatioship
body: <p>I'm trying to create a one to many relationship between <code>Category</code> and <code>Services</code> models.</p>
  <p>I have two schemas: <code>Category</code> and <code>Service</code>.</p>
  <p><code>Category</code> schema holds <code>services</code> array.</p>
  <p><code>Service</code> schema hold <code>category</code> _id.</p>
  <p>I'm trying to retrieve all <code>Categories</code> including <code>services</code>.</p>
  <p><code>Category</code> schema:</p>
  <pre><code>import mongoose from 'mongoose';
  
  const categoriesModel = new mongoose.Schema(
      {
          category: {
              type: String,
              required: true,
          },
          services: [{ type: mongoose.SchemaTypes.ObjectId, ref: 'Service' }],
      },
      { timestamps: true }
  );
  
  export const Category = mongoose.model('Category', categoriesModel);
  </code></pre>
  <p><code>Service</code> schema:</p>
  <pre><code>import mongoose from 'mongoose';
  
  const serviceSchema = new mongoose.Schema(
      {
          serviceName: {
              type: String,
              required: true,
          },
          description: {
              type: String,
              required: true,
          },
          images: {
              type: Array,
              image: Buffer,
              required: true,
          },
          category: {
              type: mongoose.SchemaTypes.ObjectId,
              ref: 'Category',
              required: true,
          },
      },
      { timestamps: true }
  );
  
  export const Service = mongoose.model('Service', serviceSchema);
  </code></pre>
  <p>HTTP get request to retrieve all <code>Categories</code> including <code>Services</code>:</p>
  <pre><code>export const getAllCategories = async (req, res) =&gt; {
      try {
          const docs = await Category.find({}).populate({
              path: 'services',
              model: 'Service',
          });
          console.log(docs);
          res.status(200).json({ data: docs });
      } catch (err) {
          res.status(400).json(err.message);
      }
  };
  </code></pre>
  <p>This is what I get as a response from the request above:</p>
  <pre><code>  {
              &quot;services&quot;: [],
              &quot;_id&quot;: &quot;60affe06c71901281d3d820d&quot;,
              &quot;category&quot;: &quot;Electricity&quot;
    },
  </code></pre>
  <p>I've tried different ways to <code>populate</code> <code>services</code> but none of them worked.</p>
  <p>What I've tried so far:</p>
  <ol>
  <li><p>Add additional nesting to <code>services</code> attribute.</p>
  </li>
  <li><p>Adding object to the <code>populate</code> params:</p>
  <pre><code>{
           path: 'services',
           model: 'Service',
  }
  </code></pre>
  </li>
  <li><p>Removing existing data ant posting additional one.</p>
  </li>
  </ol>
  <p>Fun fact. Retrieving each <code>Service</code> individually, <code>service</code> data includes the actual category, that it's been assigned to:</p>
  <pre><code> export const getAllServices = async (req, res) =&gt; {
          try {
              const doc = await Service.find({}).populate('category');
              res.status(200).json({ data: doc });
          } catch (err) {
              res.status(400).json(err);
          }
      };
  </code></pre>
  <p>Is there something I'm missing regarding including <code>services</code> within retrieving <code>categories</code>? It's really interesting, because somehow the actual <code>services</code> data is <code>[]</code>.</p>
  <p><strong>EDIT</strong>: Each <code>category</code> in <code>Categories</code> collection has empty <code>services</code> array. The <code>services</code> are added dynamically, they might be <code>null</code> for specific <code>category</code> until user adds a service and assigns a <code>category</code> to it. Is this forbidden? Do I need to find specific <code>category</code> and add a specific <code>service</code> to <code>category</code> while creating new <code>service</code>? If yes, this would require to add additional <code>Category</code> model to the <code>services.controller.js</code> and find the <code>category</code>. Is this really an appropriate solution?</p>