<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jahid Mridha</title>
    <description>The latest articles on DEV Community by Jahid Mridha (@mridha).</description>
    <link>https://dev.to/mridha</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F707274%2Fabfc9175-ecfa-4b46-84d8-7a0e06cf0a57.jpg</url>
      <title>DEV Community: Jahid Mridha</title>
      <link>https://dev.to/mridha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mridha"/>
    <language>en</language>
    <item>
      <title>What is ref in mongoose.js</title>
      <dc:creator>Jahid Mridha</dc:creator>
      <pubDate>Mon, 26 Dec 2022 14:40:46 +0000</pubDate>
      <link>https://dev.to/mridha/what-is-ref-in-mongoosejs-3j15</link>
      <guid>https://dev.to/mridha/what-is-ref-in-mongoosejs-3j15</guid>
      <description>&lt;p&gt;In Mongoose, a '&lt;strong&gt;ref&lt;/strong&gt;' is a property of a schema type that specifies the MongoDB collection in which the referenced documents live. It is used to create a "relationship" between two or more collections in a &lt;strong&gt;MongoDB&lt;/strong&gt; database.&lt;/p&gt;

&lt;p&gt;For example, suppose you have two collections in your MongoDB database: "users" and "posts". If you want to associate each "post" document with the "user" who wrote it, you can do so by using the '&lt;strong&gt;ref&lt;/strong&gt;' property in your Mongoose schema.&lt;/p&gt;

&lt;p&gt;Here is an example of how you might use the '&lt;strong&gt;ref&lt;/strong&gt;' property in a Mongoose schema to create a relationship between the "users" and "posts" collections:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const userSchema = new mongoose.Schema({
  name: String,
  email: String
});

const postSchema = new mongoose.Schema({
  title: String,
  body: String,
  author: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User'
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the '&lt;strong&gt;author&lt;/strong&gt;' field of the "post" schema is an '&lt;strong&gt;ObjectId&lt;/strong&gt;' that references a document in the "users" collection. The '&lt;strong&gt;ref&lt;/strong&gt;' property specifies that the '&lt;strong&gt;author&lt;/strong&gt;' field is a reference to a document in the "users" collection.&lt;/p&gt;

&lt;p&gt;You can then use the '&lt;strong&gt;populate()&lt;/strong&gt;' method to populate the '&lt;strong&gt;author&lt;/strong&gt;' field with the actual user document, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Post.find().populate('author').exec((err, posts) =&amp;gt; {
  // `posts` is an array of post documents with the associated user documents
  // embedded in the `author` field
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope this helps! Let me know if you have any questions.&lt;/p&gt;

</description>
      <category>career</category>
      <category>mentorship</category>
      <category>workplace</category>
    </item>
  </channel>
</rss>
