title: Mongo query operation for matching two elements within same index
body: <p><strong>Scope of Project</strong>: I am making a newsfeed, like Facebook. And I have a like button feature that will add a like to a post item when clicked.</p>
  
  <p><strong>Problem</strong> Lets say I have two posts, post A and post B. </p>
  
  <p>If I like post A and like again then my server returns "User already liked post", okay that works.</p>
  
  <p>However, if I like post B then the server returns the same "User already liked post"</p>
  
  <p><strong>Query:</strong></p>
  
  <pre><code>Feed.findOne({
        owner: req.body.authorId,
        $and: [
          {
            "posts.likes.likeList": {
              $elemMatch: { user: req.user._id }
            }
          },
          { posts: { $elemMatch: { _id: req.body.postId } } }
        ]
      }).then(checkedFeed =&gt; {
        if (checkedFeed) {
          return res.status(400).json({ Error: "User has already liked post" });
        }
  </code></pre>
  
  <p><strong>What I think the problem is</strong> When the user likes post B while post A is liked, the <code>$and</code> operator matches the <code>req.user._id</code> with the <code>posts.likes.likeList</code> of post A in the first index of the <code>$and</code> array. Then, it matches the <code>_id</code> of <code>posts</code> in the second  index of the <code>$and</code> array. And then returns the entire feed as a match.</p>
  
  <p>So, if I am correct in this regards, how do I write a query that matches the post id (the second index of the <code>$and</code> array) with the <code>post.likes.likeList</code> list of users?</p>
  
  <p><strong>Schema</strong></p>
  
  <pre><code>{
    owner: {
      type: Schema.Types.ObjectId,
      ref: "userType"
    },
    posts: [
      {
        likes: {
          totalLikes: { type: Number, default: 0 },
          likeList: [
            {
              user: { type: Schema.Types.ObjectId, ref: "User" },
              avatar: { type: String },
              name: { type: String },
              date: {
                type: Date,
                default: Date.now
              }
            }
          ]
        }
  });
  </code></pre>
  
  <p><strong>Test Data*</strong></p>
  
  <pre><code>    {
  
  //POST B &lt;-------
          "_id" : ObjectId("5d0a61bc5b835b2428289c1b"),
          "owner" : ObjectId("5c9bf6eb1da18b038ca660b8"),
          "posts" : [ 
              {
                  "likes" : {
                      "totalLikes" : 0,
                      "likeList" : []
                  },
                  "_id" : ObjectId("5d0a61bc5b835b2428289c1c"),
                  "postBody" : "Test text only",
                  "author" : {
                      "userType" : "User",
                      "user" : ObjectId("5c9bf6eb1da18b038ca660b8"),
                      "name" : "Amari DeFrance",
                      "avatar" : "https://stemuli.blob.core.windows.net/stemuli/profile-picture-e1367a7a-41c2-4ab4-9cb5-621d2008260f.jpg"
                  }
              }, 
              {
  //Post A &lt;------
                  "likes" : {
                      "totalLikes" : 1,
                      "likeList" : [ 
                          {
                              "_id" : ObjectId("5d0a66efbac13b4ff8b3b1c8"),
                              "user" : ObjectId("5c9bf6eb1da18b038ca660b8"),
                              "avatar" : "https://stemuli.blob.core.windows.net/stemuli/profile-picture-e1367a7a-41c2-4ab4-9cb5-621d2008260f.jpg",
                              "name" : "Amari DeFrance",
                              "date" : ISODate("2019-06-19T16:46:39.177Z")
                          }
                      ]
                  },
                  "postBody" : "Test photo",
                  "author" : {
                      "userType" : "User",
                      "user" : ObjectId("5c9bf6eb1da18b038ca660b8"),
                      "name" : "Amari DeFrance",
                      "avatar" : "https://stemuli.blob.core.windows.net/stemuli/profile-picture-e1367a7a-41c2-4ab4-9cb5-621d2008260f.jpg"
                  },
                  "date" : ISODate("2019-06-19T16:25:26.123Z")
              }
          ],
          "__v" : 3
      }
  </code></pre>
  
  <p><strong>New Query per suggested answer</strong></p>
  
  <pre><code>  Feed.aggregate([
        {
          $match: {
            $expr: {
              $and: [
                {
                  $eq: ["$owner", req.body.authorId]
                },
                {
                  $anyElementTrue: {
                    $map: {
                      input: "$posts",
                      in: {
                        $and: [
                          {
                            $eq: ["$$this._id", req.body.postId]
                          },
                          {
                            $anyElementTrue: {
                              $map: {
                                input: "$$this.likes.likeList",
                                as: "like",
                                in: {
                                  $eq: ["$$like.user", req.user._id]
                                }
                              }
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      ]).then(checkedFeed =&gt; {
        if (checkedFeed.length !== 0) {
          return res.status(400).json({ Error: "User has already liked post" });
        }
  </code></pre>
  
  <p><a href="https://mongoplayground.net/p/_UODjHiIiiH" rel="nofollow noreferrer">MongoDB query test with post B having liked post from the user</a></p>