0

I want to find the document with conditions and query but it always returns one document instead of giving zero: There is no user_id field exists with the given data '595de1cba036e21748b016d3', I want to find a document with this condition even key is not exists.

req.body.id = "595de80fb491b411b46afc6b";
req.body.comment_id = "595e15425eb0971820fef478";
req.body.sessionUserId = "595de1cba036e21748b016d3";

Post.find({
            $and: [
                        {_id: req.body.id},
                        {"comments._id": req.body.comment_id},
                        {"comments.comment_like.user_id": req.body.sessionUserId}
                    ]
                }
            ).exec(function (err, docs) {
     })

Please find the query error.

Here is my document:

{
    "_id" : ObjectId("595de80fb491b411b46afc6b"),
    "text" : "Hic nostrud cumque qui eos excepturi ut debitis distinctio Repudiandae",
    "user_id" : "595de7ccb491b411b46afc6a",
    "discussion_image" : "",
    "user_pic" : "https://s3.amazonaws.com/1499326458943.jpg",
    "user_name" : "Seth Nieves",
    "date" : ISODate("2017-07-06T13:04:39.177+05:30"),
    "comment_count" : 0,
    "like" : [ 
        {
            "status" : true,
            "date" : ISODate("2017-07-06T16:13:32.156+05:30"),
            "_id" : ObjectId("595e14ec5eb0971820fef473"),
            "user_id" : "595de7ccb491b411b46afc6a"
        }
    ],
    "comments" : [ 
        {
            "comment_like" : [],
            "_id" : ObjectId("595e14b25eb0971820fef465"),
            "commented_date" : ISODate("2017-07-06T16:15:06.967+05:30"),
            "commented_user_picture" : "https://s3.amazonaws.com/1499324969183.jpg",
            "commented_user_name" : "Ashwani Cochran",
            "discussion_id" : "595de80fb491b411b46afc6b",
            "text" : "Est ut iusto iusto in sit vitae aut sed aspernatur proident anim earum dolorem quidem nostrum asperiores ea",
            "commented_user_id" : "595de1cba036e21748b016d3"
        }, 
        {
            "comment_like" : [ 
                {
                    "status" : true,
                    "date" : ISODate("2017-07-06T18:46:27.684+05:30"),
                    "_id" : ObjectId("595e3b997f35d21e480f19aa"),
                    "user_id" : "595de7ccb491b411b46afc6a"
                }
            ],
            "_id" : ObjectId("595e14e35eb0971820fef469"),
            "commented_date" : ISODate("2017-07-06T16:15:55.590+05:30"),
            "commented_user_picture" : "https://s3.amazonaws.com/1499326458943.jpg",
            "commented_user_name" : "Seth Nieves",
            "discussion_id" : "595de80fb491b411b46afc6b",
            "text" : "Sint sapiente magnam omnis vel laudantium",
            "commented_user_id" : "595de7ccb491b411b46afc6a"
        }, 
        {
            "comment_like" : [ 
                {
                    "status" : true,
                    "date" : ISODate("2017-07-07T10:30:40.779+05:30"),
                    "_id" : ObjectId("595f1874adf8c11214bef2cd"),
                    "user_id" : "595de1cba036e21748b016d3"
                }
            ],
            "_id" : ObjectId("595e15425eb0971820fef478"),
            "commented_date" : ISODate("2017-07-06T16:17:30.951+05:30"),
            "commented_user_picture" : "https://s3.amazonaws.com/1499324969183.jpg",
            "commented_user_name" : "Ashwani Cochran",
            "discussion_id" : "595de80fb491b411b46afc6b",
            "text" : "Sed aliquip similique pariatur Dolore facere voluptatem Aspernatur",
            "commented_user_id" : "595de1cba036e21748b016d3"
        }, 
        {
            "comment_like" : [],
            "_id" : ObjectId("595e16385eb0971820fef47d"),
            "commented_date" : ISODate("2017-07-06T16:21:36.074+05:30"),
            "commented_user_picture" : "https://s3.amazonaws.com/1499324969183.jpg",
            "commented_user_name" : "Ashwani Cochran",
            "discussion_id" : "595de80fb491b411b46afc6b",
            "text" : "Laudantium autem ipsam sunt id qui obcaecati non eos dolorem sunt laboris obcaecati accusantium fuga Qui cillum sed et",
            "commented_user_id" : "595de1cba036e21748b016d3"
        }
    ],
    "__v" : 0
}
  • Post.find({_id: req.body.id, "comments._id": req.body.comment_id, "comments.comment_like.user_id": req.body.sessionUserId}).exec(function(err, docs){}) – Ankit Kumar Jul 07 '17 at 06:03
  • 1
    You need [`$elemMatch`](https://docs.mongodb.com/manual/reference/operator/query/elemMatch/) when supplying "multiple criteria" for a specific array element. Without that the "whole" array is considered for the tested values rather than just one element. `.find({ "_id": req.body.id, "comments": { "$elemMatch": { "_id": req.body.comment_id, "user_id": req.body.sessionUserId } } })` – Neil Lunn Jul 07 '17 at 06:06
  • @Ankit No. Read my comment and [the matching answer to that in the linked duplicate](https://stackoverflow.com/a/20727505/2313887). This is where and why you use `$elemMatch` – Neil Lunn Jul 07 '17 at 06:08
  • Thanks @NeilLunn It works! – Ashwani Kumar Jul 07 '17 at 07:15

0 Answers0