Mongo query to fetch selective fields from the collection [duplicate]












-2
















This question already has an answer here:




  • How to select a single field in MongoDB?

    15 answers




Is it possible to fetch selective fields from mongo collections.



Say if my collections is



USer_info

{"_id" : ObjectId("56e0a3a2d59feaa43fba49d5", "date":"2018-07-23T00:00:00.000Z", "Name":"ABC", "ID": "1234", "Subjects":"[Maths, Physics, Science, Music]"}
{"_id" : ObjectId("56e0a3a2d5hjhfjtaese45d5", "date":"2018-07-23T00:00:00.000Z", "Name":"BCS", "ID": "3456", "Subjects":"[CS, Physics, Business]"}
{"_id" : ObjectId("56e0a3a2d59feaa43fba49d6", "date":"2018-07-23T00:00:00.000Z", "Name":"XYZ", "ID": "786", "Subjects": "[Agricultar, Business, Music]"}


Query to list only Name and SUbjects fields:



{"Name":"ABC", "Subjects":"[Maths, Physics, Science, Music]"}
{"Name":"BCS", "Subjects":"[CS, Physics, Business]"}
{"Name":"XYZ", "Subjects": "[Agricultar, Business, Music]"}


Not sure if this a duplicate, but i did not get anything similar so posting it.










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 24 '18 at 9:31


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.























    -2
















    This question already has an answer here:




    • How to select a single field in MongoDB?

      15 answers




    Is it possible to fetch selective fields from mongo collections.



    Say if my collections is



    USer_info

    {"_id" : ObjectId("56e0a3a2d59feaa43fba49d5", "date":"2018-07-23T00:00:00.000Z", "Name":"ABC", "ID": "1234", "Subjects":"[Maths, Physics, Science, Music]"}
    {"_id" : ObjectId("56e0a3a2d5hjhfjtaese45d5", "date":"2018-07-23T00:00:00.000Z", "Name":"BCS", "ID": "3456", "Subjects":"[CS, Physics, Business]"}
    {"_id" : ObjectId("56e0a3a2d59feaa43fba49d6", "date":"2018-07-23T00:00:00.000Z", "Name":"XYZ", "ID": "786", "Subjects": "[Agricultar, Business, Music]"}


    Query to list only Name and SUbjects fields:



    {"Name":"ABC", "Subjects":"[Maths, Physics, Science, Music]"}
    {"Name":"BCS", "Subjects":"[CS, Physics, Business]"}
    {"Name":"XYZ", "Subjects": "[Agricultar, Business, Music]"}


    Not sure if this a duplicate, but i did not get anything similar so posting it.










    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 24 '18 at 9:31


    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.





















      -2












      -2








      -2









      This question already has an answer here:




      • How to select a single field in MongoDB?

        15 answers




      Is it possible to fetch selective fields from mongo collections.



      Say if my collections is



      USer_info

      {"_id" : ObjectId("56e0a3a2d59feaa43fba49d5", "date":"2018-07-23T00:00:00.000Z", "Name":"ABC", "ID": "1234", "Subjects":"[Maths, Physics, Science, Music]"}
      {"_id" : ObjectId("56e0a3a2d5hjhfjtaese45d5", "date":"2018-07-23T00:00:00.000Z", "Name":"BCS", "ID": "3456", "Subjects":"[CS, Physics, Business]"}
      {"_id" : ObjectId("56e0a3a2d59feaa43fba49d6", "date":"2018-07-23T00:00:00.000Z", "Name":"XYZ", "ID": "786", "Subjects": "[Agricultar, Business, Music]"}


      Query to list only Name and SUbjects fields:



      {"Name":"ABC", "Subjects":"[Maths, Physics, Science, Music]"}
      {"Name":"BCS", "Subjects":"[CS, Physics, Business]"}
      {"Name":"XYZ", "Subjects": "[Agricultar, Business, Music]"}


      Not sure if this a duplicate, but i did not get anything similar so posting it.










      share|improve this question















      This question already has an answer here:




      • How to select a single field in MongoDB?

        15 answers




      Is it possible to fetch selective fields from mongo collections.



      Say if my collections is



      USer_info

      {"_id" : ObjectId("56e0a3a2d59feaa43fba49d5", "date":"2018-07-23T00:00:00.000Z", "Name":"ABC", "ID": "1234", "Subjects":"[Maths, Physics, Science, Music]"}
      {"_id" : ObjectId("56e0a3a2d5hjhfjtaese45d5", "date":"2018-07-23T00:00:00.000Z", "Name":"BCS", "ID": "3456", "Subjects":"[CS, Physics, Business]"}
      {"_id" : ObjectId("56e0a3a2d59feaa43fba49d6", "date":"2018-07-23T00:00:00.000Z", "Name":"XYZ", "ID": "786", "Subjects": "[Agricultar, Business, Music]"}


      Query to list only Name and SUbjects fields:



      {"Name":"ABC", "Subjects":"[Maths, Physics, Science, Music]"}
      {"Name":"BCS", "Subjects":"[CS, Physics, Business]"}
      {"Name":"XYZ", "Subjects": "[Agricultar, Business, Music]"}


      Not sure if this a duplicate, but i did not get anything similar so posting it.





      This question already has an answer here:




      • How to select a single field in MongoDB?

        15 answers








      mongodb mongodb-query pymongo






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 18:17









      RachelRachel

      949




      949




      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 24 '18 at 9:31


      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 24 '18 at 9:31


      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.


























          1 Answer
          1






          active

          oldest

          votes


















          1














          You have to define projection part of .find() method to specify which fields should be returned. You alsow have to explicitly exclude _id, try:



          col.find({}, { "_id": 0, "Name": 1, "Subjects": 1 })





          share|improve this answer






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You have to define projection part of .find() method to specify which fields should be returned. You alsow have to explicitly exclude _id, try:



            col.find({}, { "_id": 0, "Name": 1, "Subjects": 1 })





            share|improve this answer




























              1














              You have to define projection part of .find() method to specify which fields should be returned. You alsow have to explicitly exclude _id, try:



              col.find({}, { "_id": 0, "Name": 1, "Subjects": 1 })





              share|improve this answer


























                1












                1








                1







                You have to define projection part of .find() method to specify which fields should be returned. You alsow have to explicitly exclude _id, try:



                col.find({}, { "_id": 0, "Name": 1, "Subjects": 1 })





                share|improve this answer













                You have to define projection part of .find() method to specify which fields should be returned. You alsow have to explicitly exclude _id, try:



                col.find({}, { "_id": 0, "Name": 1, "Subjects": 1 })






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 18:28









                micklmickl

                13.6k51536




                13.6k51536

















                    Popular posts from this blog

                    Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                    Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

                    A Topological Invariant for $pi_3(U(n))$