Query to get document along with multiple sub document count in MongoDB [duplicate]










0
















This question already has an answer here:



  • MongoDB: count the number of items in an array

    2 answers



  • Retrieve only the queried element in an object array in MongoDB collection

    11 answers



I am really new in MongoDB and working on this using Mongoose. Basically, I am working on project which is developing on Node JS, MongoDB with Mongoose.



I have posts collection having data in below format:




"_id": ObjectId("5beb9f4476034d750c8b1b02"),
"message": "My Favourite place is my home",
"media": "",
"deleted_at": null,
"user_id": ObjectId("5be72e8899e40d0494546c2a"),
"likes": [

"_id": ObjectId("5bebaa20bdb5e405ce6584ff"),
"user_id": ObjectId("5be72cbe24babb7f94c395e1")

],
"comments": [

"deleted_at": null,
"_id": ObjectId("5bebaebd3e13290c5bcac8bd"),
"user_id": ObjectId("5be72cbe24babb7f94c395e1"),
"comment": "Me Too",
"created_at": ISODate("2018-11-14T05:12:29.441Z"),
"updated_at": ISODate("2018-11-14T05:12:29.441Z")
,

"deleted_at": null,
"_id": ObjectId("5bebaf226c22480ce824ea05"),
"user_id": ObjectId("5be72cbe24babb7f94c395e1"),
"comment": "Lets have a party at home",
"created_at": ISODate("2018-11-14T05:14:10.237Z"),
"updated_at": ISODate("2018-11-14T05:14:10.237Z")

],
"created_at": ISODate("2018-11-14T04:06:28.198Z"),
"updated_at": ISODate("2018-11-14T04:06:28.198Z"),
"__v": 0




The above one just a sample data of only 1 document. My collection will have multiple document with likes and comments.



Now, I want to fetch all posts document, which has posted by either me or my friends along with count of likes and comments on respective post. In comments sub document, only those comment should be count whose deleted_at value is null or blank.



My friend collection is given below:




"_id": ObjectId("5be745262aa9a826b4bc21ab"),
"status": 1,
"user_id_1": ObjectId("5be72cbe24babb7f94c395e1"),
"user_id_2": ObjectId("5be72e8899e40d0494546c2a"),
"action_user_id": ObjectId("5be72e8899e40d0494546c2a"),
"greet_message": "want to be friend",
"created_at": ISODate("2018-11-10T20:52:54.261Z"),
"updated_at": ISODate("2018-11-10T20:52:54.261Z"),
"__v": 0




Now, I am not able to think about how can I make query in such a way so that, I can have all the posts posted by me or by my friends along with total likes and comments. Please help me on this.



I know, there are already similar questions has been posted on this platform but I am not able to get much things from them. I need following things in my scenario:



  1. Get all the posts posted by either me or by my friends.

  2. Also want to get total likes and total comments on each post. Also want to know about, If I want to count of likes and comments on condition basis then what will be the query. For example, I want count of comments put by specific user.

  3. I also want to get 1 more extra fields that contain status of whether I have liked this post or not.

These are some questions that I need to fulfill in single query but I have not got anything regarding the same anywhere.



Apart from this, If possible then please I would like to also get extra field in post document that can tell us that, this post was already liked by me or not.



Can anyone share the query for this scenario? I am using node js with mongoose.



Thanks










share|improve this question















marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 6:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






















    0
















    This question already has an answer here:



    • MongoDB: count the number of items in an array

      2 answers



    • Retrieve only the queried element in an object array in MongoDB collection

      11 answers



    I am really new in MongoDB and working on this using Mongoose. Basically, I am working on project which is developing on Node JS, MongoDB with Mongoose.



    I have posts collection having data in below format:




    "_id": ObjectId("5beb9f4476034d750c8b1b02"),
    "message": "My Favourite place is my home",
    "media": "",
    "deleted_at": null,
    "user_id": ObjectId("5be72e8899e40d0494546c2a"),
    "likes": [

    "_id": ObjectId("5bebaa20bdb5e405ce6584ff"),
    "user_id": ObjectId("5be72cbe24babb7f94c395e1")

    ],
    "comments": [

    "deleted_at": null,
    "_id": ObjectId("5bebaebd3e13290c5bcac8bd"),
    "user_id": ObjectId("5be72cbe24babb7f94c395e1"),
    "comment": "Me Too",
    "created_at": ISODate("2018-11-14T05:12:29.441Z"),
    "updated_at": ISODate("2018-11-14T05:12:29.441Z")
    ,

    "deleted_at": null,
    "_id": ObjectId("5bebaf226c22480ce824ea05"),
    "user_id": ObjectId("5be72cbe24babb7f94c395e1"),
    "comment": "Lets have a party at home",
    "created_at": ISODate("2018-11-14T05:14:10.237Z"),
    "updated_at": ISODate("2018-11-14T05:14:10.237Z")

    ],
    "created_at": ISODate("2018-11-14T04:06:28.198Z"),
    "updated_at": ISODate("2018-11-14T04:06:28.198Z"),
    "__v": 0




    The above one just a sample data of only 1 document. My collection will have multiple document with likes and comments.



    Now, I want to fetch all posts document, which has posted by either me or my friends along with count of likes and comments on respective post. In comments sub document, only those comment should be count whose deleted_at value is null or blank.



    My friend collection is given below:




    "_id": ObjectId("5be745262aa9a826b4bc21ab"),
    "status": 1,
    "user_id_1": ObjectId("5be72cbe24babb7f94c395e1"),
    "user_id_2": ObjectId("5be72e8899e40d0494546c2a"),
    "action_user_id": ObjectId("5be72e8899e40d0494546c2a"),
    "greet_message": "want to be friend",
    "created_at": ISODate("2018-11-10T20:52:54.261Z"),
    "updated_at": ISODate("2018-11-10T20:52:54.261Z"),
    "__v": 0




    Now, I am not able to think about how can I make query in such a way so that, I can have all the posts posted by me or by my friends along with total likes and comments. Please help me on this.



    I know, there are already similar questions has been posted on this platform but I am not able to get much things from them. I need following things in my scenario:



    1. Get all the posts posted by either me or by my friends.

    2. Also want to get total likes and total comments on each post. Also want to know about, If I want to count of likes and comments on condition basis then what will be the query. For example, I want count of comments put by specific user.

    3. I also want to get 1 more extra fields that contain status of whether I have liked this post or not.

    These are some questions that I need to fulfill in single query but I have not got anything regarding the same anywhere.



    Apart from this, If possible then please I would like to also get extra field in post document that can tell us that, this post was already liked by me or not.



    Can anyone share the query for this scenario? I am using node js with mongoose.



    Thanks










    share|improve this question















    marked as duplicate by Neil Lunn mongodb
    Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

    StackExchange.ready(function()
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function()
    $hover.showInfoMessage('',
    messageElement: $msg.clone().show(),
    transient: false,
    position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
    dismissable: false,
    relativeToBody: true
    );
    ,
    function()
    StackExchange.helpers.removeMessages();

    );
    );
    );
    Nov 15 '18 at 6:23


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















      0












      0








      0









      This question already has an answer here:



      • MongoDB: count the number of items in an array

        2 answers



      • Retrieve only the queried element in an object array in MongoDB collection

        11 answers



      I am really new in MongoDB and working on this using Mongoose. Basically, I am working on project which is developing on Node JS, MongoDB with Mongoose.



      I have posts collection having data in below format:




      "_id": ObjectId("5beb9f4476034d750c8b1b02"),
      "message": "My Favourite place is my home",
      "media": "",
      "deleted_at": null,
      "user_id": ObjectId("5be72e8899e40d0494546c2a"),
      "likes": [

      "_id": ObjectId("5bebaa20bdb5e405ce6584ff"),
      "user_id": ObjectId("5be72cbe24babb7f94c395e1")

      ],
      "comments": [

      "deleted_at": null,
      "_id": ObjectId("5bebaebd3e13290c5bcac8bd"),
      "user_id": ObjectId("5be72cbe24babb7f94c395e1"),
      "comment": "Me Too",
      "created_at": ISODate("2018-11-14T05:12:29.441Z"),
      "updated_at": ISODate("2018-11-14T05:12:29.441Z")
      ,

      "deleted_at": null,
      "_id": ObjectId("5bebaf226c22480ce824ea05"),
      "user_id": ObjectId("5be72cbe24babb7f94c395e1"),
      "comment": "Lets have a party at home",
      "created_at": ISODate("2018-11-14T05:14:10.237Z"),
      "updated_at": ISODate("2018-11-14T05:14:10.237Z")

      ],
      "created_at": ISODate("2018-11-14T04:06:28.198Z"),
      "updated_at": ISODate("2018-11-14T04:06:28.198Z"),
      "__v": 0




      The above one just a sample data of only 1 document. My collection will have multiple document with likes and comments.



      Now, I want to fetch all posts document, which has posted by either me or my friends along with count of likes and comments on respective post. In comments sub document, only those comment should be count whose deleted_at value is null or blank.



      My friend collection is given below:




      "_id": ObjectId("5be745262aa9a826b4bc21ab"),
      "status": 1,
      "user_id_1": ObjectId("5be72cbe24babb7f94c395e1"),
      "user_id_2": ObjectId("5be72e8899e40d0494546c2a"),
      "action_user_id": ObjectId("5be72e8899e40d0494546c2a"),
      "greet_message": "want to be friend",
      "created_at": ISODate("2018-11-10T20:52:54.261Z"),
      "updated_at": ISODate("2018-11-10T20:52:54.261Z"),
      "__v": 0




      Now, I am not able to think about how can I make query in such a way so that, I can have all the posts posted by me or by my friends along with total likes and comments. Please help me on this.



      I know, there are already similar questions has been posted on this platform but I am not able to get much things from them. I need following things in my scenario:



      1. Get all the posts posted by either me or by my friends.

      2. Also want to get total likes and total comments on each post. Also want to know about, If I want to count of likes and comments on condition basis then what will be the query. For example, I want count of comments put by specific user.

      3. I also want to get 1 more extra fields that contain status of whether I have liked this post or not.

      These are some questions that I need to fulfill in single query but I have not got anything regarding the same anywhere.



      Apart from this, If possible then please I would like to also get extra field in post document that can tell us that, this post was already liked by me or not.



      Can anyone share the query for this scenario? I am using node js with mongoose.



      Thanks










      share|improve this question

















      This question already has an answer here:



      • MongoDB: count the number of items in an array

        2 answers



      • Retrieve only the queried element in an object array in MongoDB collection

        11 answers



      I am really new in MongoDB and working on this using Mongoose. Basically, I am working on project which is developing on Node JS, MongoDB with Mongoose.



      I have posts collection having data in below format:




      "_id": ObjectId("5beb9f4476034d750c8b1b02"),
      "message": "My Favourite place is my home",
      "media": "",
      "deleted_at": null,
      "user_id": ObjectId("5be72e8899e40d0494546c2a"),
      "likes": [

      "_id": ObjectId("5bebaa20bdb5e405ce6584ff"),
      "user_id": ObjectId("5be72cbe24babb7f94c395e1")

      ],
      "comments": [

      "deleted_at": null,
      "_id": ObjectId("5bebaebd3e13290c5bcac8bd"),
      "user_id": ObjectId("5be72cbe24babb7f94c395e1"),
      "comment": "Me Too",
      "created_at": ISODate("2018-11-14T05:12:29.441Z"),
      "updated_at": ISODate("2018-11-14T05:12:29.441Z")
      ,

      "deleted_at": null,
      "_id": ObjectId("5bebaf226c22480ce824ea05"),
      "user_id": ObjectId("5be72cbe24babb7f94c395e1"),
      "comment": "Lets have a party at home",
      "created_at": ISODate("2018-11-14T05:14:10.237Z"),
      "updated_at": ISODate("2018-11-14T05:14:10.237Z")

      ],
      "created_at": ISODate("2018-11-14T04:06:28.198Z"),
      "updated_at": ISODate("2018-11-14T04:06:28.198Z"),
      "__v": 0




      The above one just a sample data of only 1 document. My collection will have multiple document with likes and comments.



      Now, I want to fetch all posts document, which has posted by either me or my friends along with count of likes and comments on respective post. In comments sub document, only those comment should be count whose deleted_at value is null or blank.



      My friend collection is given below:




      "_id": ObjectId("5be745262aa9a826b4bc21ab"),
      "status": 1,
      "user_id_1": ObjectId("5be72cbe24babb7f94c395e1"),
      "user_id_2": ObjectId("5be72e8899e40d0494546c2a"),
      "action_user_id": ObjectId("5be72e8899e40d0494546c2a"),
      "greet_message": "want to be friend",
      "created_at": ISODate("2018-11-10T20:52:54.261Z"),
      "updated_at": ISODate("2018-11-10T20:52:54.261Z"),
      "__v": 0




      Now, I am not able to think about how can I make query in such a way so that, I can have all the posts posted by me or by my friends along with total likes and comments. Please help me on this.



      I know, there are already similar questions has been posted on this platform but I am not able to get much things from them. I need following things in my scenario:



      1. Get all the posts posted by either me or by my friends.

      2. Also want to get total likes and total comments on each post. Also want to know about, If I want to count of likes and comments on condition basis then what will be the query. For example, I want count of comments put by specific user.

      3. I also want to get 1 more extra fields that contain status of whether I have liked this post or not.

      These are some questions that I need to fulfill in single query but I have not got anything regarding the same anywhere.



      Apart from this, If possible then please I would like to also get extra field in post document that can tell us that, this post was already liked by me or not.



      Can anyone share the query for this scenario? I am using node js with mongoose.



      Thanks





      This question already has an answer here:



      • MongoDB: count the number of items in an array

        2 answers



      • Retrieve only the queried element in an object array in MongoDB collection

        11 answers







      mongodb mongoose






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 '18 at 7:07







      Dhirender

















      asked Nov 14 '18 at 19:13









      DhirenderDhirender

      324112




      324112




      marked as duplicate by Neil Lunn mongodb
      Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Nov 15 '18 at 6:23


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Neil Lunn mongodb
      Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Nov 15 '18 at 6:23


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          0






          active

          oldest

          votes

















          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes

          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3