title: mongo db - schema for implementing like feature in webapp
body: <p>i have a mongodb database, in which i got 2 collections. <code>posts</code> and <code>users</code>. </p>
  
  <p>posts json structure is like</p>
  
  <pre><code>{title:"Title", content:"content goes here", postedby: "userid"}
  </code></pre>
  
  <p>and users is like</p>
  
  <pre><code>{username:"", name:""}
  </code></pre>
  
  <p>Now i need to implement a like feature, where users like the posts.</p>
  
  <p><strong>Solution 1</strong></p>
  
  <p>i can put an inner array in users like </p>
  
  <pre><code>{username:"", name:"", likes:[postid1,postid2..]}
  </code></pre>
  
  <p>issue here is its easy to query posts which a user liked. But difficult to get people who liked an article.</p>
  
  <p><strong>Solution 2</strong></p>
  
  <p>i can put an inner array in posts like</p>
  
  <pre><code>{title:"Title", content:"content goes here", postedby: "userid", like:[userid1,userid2 ..]}
  </code></pre>
  
  <p>issue here is its easy to get people who liked an article. But difficult to query posts which a user liked.</p>
  
  <p>How can i tackle this?
  currently i am thinking about having both the ways. Just like keeping inner arrays in both collections. I know i am keeping redundant data, is it the best way to approach this issue?</p>