node mongodb 3.4 using find with specify fields and toArray does not filter results [duplicate]












1















This question already has an answer here:




  • batchSize field name ignored in Field Projection

    2 answers




Again, using mongo v3.4 and referencing these docs: https://docs.mongodb.com/v3.4/tutorial/project-fields-from-query-results/



My example looks like:



    const m = this.getCollection(SOME_COLLECTION);
m.find({
'_id': {
$nin: [Ace, Bay],
},
'value.someCategory': {
$type: 'object',
},
}, {
'_id': 0,
'value.someCategory': 1,
}).toArray((err, doc) => {
if (err) {
console.log(err);
} else {
console.log(doc);
}
});


My doc array will return all items that follow my filter of value.someCategory of type object, but will not remove the _id and will return all fields even though I'd like to specify only the value.someCategory field.



Example data in mongo:



[
{
_id: 'hello',
value: {
someCategory: [Object],
name: 'hello',
otherCategory: true,
}
},
{
_id: 'Ace',
value: {
someCategory: [Object],
name: 'Ace',
otherCategory: true,
}
},
{
_id: 'testing',
value: {
someCategory: null,
name: 'testing',
otherCategory: true,
}
},
]


And expect the result to be:



[
{
someCategory: [Object],
},
]


Based on the docs linked above, the specified fields should be the second param. I'm wondering now if the use of toArray affects the return value?










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 21 '18 at 19:58


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















    This question already has an answer here:




    • batchSize field name ignored in Field Projection

      2 answers




    Again, using mongo v3.4 and referencing these docs: https://docs.mongodb.com/v3.4/tutorial/project-fields-from-query-results/



    My example looks like:



        const m = this.getCollection(SOME_COLLECTION);
    m.find({
    '_id': {
    $nin: [Ace, Bay],
    },
    'value.someCategory': {
    $type: 'object',
    },
    }, {
    '_id': 0,
    'value.someCategory': 1,
    }).toArray((err, doc) => {
    if (err) {
    console.log(err);
    } else {
    console.log(doc);
    }
    });


    My doc array will return all items that follow my filter of value.someCategory of type object, but will not remove the _id and will return all fields even though I'd like to specify only the value.someCategory field.



    Example data in mongo:



    [
    {
    _id: 'hello',
    value: {
    someCategory: [Object],
    name: 'hello',
    otherCategory: true,
    }
    },
    {
    _id: 'Ace',
    value: {
    someCategory: [Object],
    name: 'Ace',
    otherCategory: true,
    }
    },
    {
    _id: 'testing',
    value: {
    someCategory: null,
    name: 'testing',
    otherCategory: true,
    }
    },
    ]


    And expect the result to be:



    [
    {
    someCategory: [Object],
    },
    ]


    Based on the docs linked above, the specified fields should be the second param. I'm wondering now if the use of toArray affects the return value?










    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 21 '18 at 19:58


    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












      1








      1








      This question already has an answer here:




      • batchSize field name ignored in Field Projection

        2 answers




      Again, using mongo v3.4 and referencing these docs: https://docs.mongodb.com/v3.4/tutorial/project-fields-from-query-results/



      My example looks like:



          const m = this.getCollection(SOME_COLLECTION);
      m.find({
      '_id': {
      $nin: [Ace, Bay],
      },
      'value.someCategory': {
      $type: 'object',
      },
      }, {
      '_id': 0,
      'value.someCategory': 1,
      }).toArray((err, doc) => {
      if (err) {
      console.log(err);
      } else {
      console.log(doc);
      }
      });


      My doc array will return all items that follow my filter of value.someCategory of type object, but will not remove the _id and will return all fields even though I'd like to specify only the value.someCategory field.



      Example data in mongo:



      [
      {
      _id: 'hello',
      value: {
      someCategory: [Object],
      name: 'hello',
      otherCategory: true,
      }
      },
      {
      _id: 'Ace',
      value: {
      someCategory: [Object],
      name: 'Ace',
      otherCategory: true,
      }
      },
      {
      _id: 'testing',
      value: {
      someCategory: null,
      name: 'testing',
      otherCategory: true,
      }
      },
      ]


      And expect the result to be:



      [
      {
      someCategory: [Object],
      },
      ]


      Based on the docs linked above, the specified fields should be the second param. I'm wondering now if the use of toArray affects the return value?










      share|improve this question
















      This question already has an answer here:




      • batchSize field name ignored in Field Projection

        2 answers




      Again, using mongo v3.4 and referencing these docs: https://docs.mongodb.com/v3.4/tutorial/project-fields-from-query-results/



      My example looks like:



          const m = this.getCollection(SOME_COLLECTION);
      m.find({
      '_id': {
      $nin: [Ace, Bay],
      },
      'value.someCategory': {
      $type: 'object',
      },
      }, {
      '_id': 0,
      'value.someCategory': 1,
      }).toArray((err, doc) => {
      if (err) {
      console.log(err);
      } else {
      console.log(doc);
      }
      });


      My doc array will return all items that follow my filter of value.someCategory of type object, but will not remove the _id and will return all fields even though I'd like to specify only the value.someCategory field.



      Example data in mongo:



      [
      {
      _id: 'hello',
      value: {
      someCategory: [Object],
      name: 'hello',
      otherCategory: true,
      }
      },
      {
      _id: 'Ace',
      value: {
      someCategory: [Object],
      name: 'Ace',
      otherCategory: true,
      }
      },
      {
      _id: 'testing',
      value: {
      someCategory: null,
      name: 'testing',
      otherCategory: true,
      }
      },
      ]


      And expect the result to be:



      [
      {
      someCategory: [Object],
      },
      ]


      Based on the docs linked above, the specified fields should be the second param. I'm wondering now if the use of toArray affects the return value?





      This question already has an answer here:




      • batchSize field name ignored in Field Projection

        2 answers








      node.js mongodb mongodb-query






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 19:21









      Anthony Winzlet

      14.2k41239




      14.2k41239










      asked Nov 21 '18 at 18:59









      philip yoophilip yoo

      1,32331326




      1,32331326




      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 21 '18 at 19:58


      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 21 '18 at 19:58


      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 Answers
          2






          active

          oldest

          votes


















          1














          Use .project cursor method instead



          db.collection('collection')
          .find({ '_id': { '$nin': [Ace, Bay] }, 'value.someCategory': { '$type': 'object' }})
          .project({ '_id': 0, 'value.someCategory': 1 })
          .toArray()





          share|improve this answer

















          • 1




            great! ty for the quick reply. I tested just now and works :) One slight difference is that the returned value is included within the key value field like: [{ value: { someCategory: [Object] }}]. Just wanted to mention this if anyone references this question/answer so they know. Thanks!
            – philip yoo
            Nov 21 '18 at 19:14






          • 1




            If you want to someCategory to top level then you have to use aggregation here. Find query can only filter and limit the data but cannot reshape the output result.
            – Anthony Winzlet
            Nov 21 '18 at 19:17










          • Ah great, good to know! Thanks for your help
            – philip yoo
            Nov 21 '18 at 19:21



















          0














          use select function and define your required field to projection.



          const m = await Model.find({
          '_id': {
          $nin: [Ace, Bay],
          },
          'value.someCategory': {
          $type: 'object',
          },
          }).select('-_id value.someCategory').lean();

          console.log(m)





          share|improve this answer





















          • Sorry, I'm not using mongoose but thanks for the answer/reply
            – philip yoo
            Nov 21 '18 at 19:30


















          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Use .project cursor method instead



          db.collection('collection')
          .find({ '_id': { '$nin': [Ace, Bay] }, 'value.someCategory': { '$type': 'object' }})
          .project({ '_id': 0, 'value.someCategory': 1 })
          .toArray()





          share|improve this answer

















          • 1




            great! ty for the quick reply. I tested just now and works :) One slight difference is that the returned value is included within the key value field like: [{ value: { someCategory: [Object] }}]. Just wanted to mention this if anyone references this question/answer so they know. Thanks!
            – philip yoo
            Nov 21 '18 at 19:14






          • 1




            If you want to someCategory to top level then you have to use aggregation here. Find query can only filter and limit the data but cannot reshape the output result.
            – Anthony Winzlet
            Nov 21 '18 at 19:17










          • Ah great, good to know! Thanks for your help
            – philip yoo
            Nov 21 '18 at 19:21
















          1














          Use .project cursor method instead



          db.collection('collection')
          .find({ '_id': { '$nin': [Ace, Bay] }, 'value.someCategory': { '$type': 'object' }})
          .project({ '_id': 0, 'value.someCategory': 1 })
          .toArray()





          share|improve this answer

















          • 1




            great! ty for the quick reply. I tested just now and works :) One slight difference is that the returned value is included within the key value field like: [{ value: { someCategory: [Object] }}]. Just wanted to mention this if anyone references this question/answer so they know. Thanks!
            – philip yoo
            Nov 21 '18 at 19:14






          • 1




            If you want to someCategory to top level then you have to use aggregation here. Find query can only filter and limit the data but cannot reshape the output result.
            – Anthony Winzlet
            Nov 21 '18 at 19:17










          • Ah great, good to know! Thanks for your help
            – philip yoo
            Nov 21 '18 at 19:21














          1












          1








          1






          Use .project cursor method instead



          db.collection('collection')
          .find({ '_id': { '$nin': [Ace, Bay] }, 'value.someCategory': { '$type': 'object' }})
          .project({ '_id': 0, 'value.someCategory': 1 })
          .toArray()





          share|improve this answer












          Use .project cursor method instead



          db.collection('collection')
          .find({ '_id': { '$nin': [Ace, Bay] }, 'value.someCategory': { '$type': 'object' }})
          .project({ '_id': 0, 'value.someCategory': 1 })
          .toArray()






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 19:08









          Anthony WinzletAnthony Winzlet

          14.2k41239




          14.2k41239








          • 1




            great! ty for the quick reply. I tested just now and works :) One slight difference is that the returned value is included within the key value field like: [{ value: { someCategory: [Object] }}]. Just wanted to mention this if anyone references this question/answer so they know. Thanks!
            – philip yoo
            Nov 21 '18 at 19:14






          • 1




            If you want to someCategory to top level then you have to use aggregation here. Find query can only filter and limit the data but cannot reshape the output result.
            – Anthony Winzlet
            Nov 21 '18 at 19:17










          • Ah great, good to know! Thanks for your help
            – philip yoo
            Nov 21 '18 at 19:21














          • 1




            great! ty for the quick reply. I tested just now and works :) One slight difference is that the returned value is included within the key value field like: [{ value: { someCategory: [Object] }}]. Just wanted to mention this if anyone references this question/answer so they know. Thanks!
            – philip yoo
            Nov 21 '18 at 19:14






          • 1




            If you want to someCategory to top level then you have to use aggregation here. Find query can only filter and limit the data but cannot reshape the output result.
            – Anthony Winzlet
            Nov 21 '18 at 19:17










          • Ah great, good to know! Thanks for your help
            – philip yoo
            Nov 21 '18 at 19:21








          1




          1




          great! ty for the quick reply. I tested just now and works :) One slight difference is that the returned value is included within the key value field like: [{ value: { someCategory: [Object] }}]. Just wanted to mention this if anyone references this question/answer so they know. Thanks!
          – philip yoo
          Nov 21 '18 at 19:14




          great! ty for the quick reply. I tested just now and works :) One slight difference is that the returned value is included within the key value field like: [{ value: { someCategory: [Object] }}]. Just wanted to mention this if anyone references this question/answer so they know. Thanks!
          – philip yoo
          Nov 21 '18 at 19:14




          1




          1




          If you want to someCategory to top level then you have to use aggregation here. Find query can only filter and limit the data but cannot reshape the output result.
          – Anthony Winzlet
          Nov 21 '18 at 19:17




          If you want to someCategory to top level then you have to use aggregation here. Find query can only filter and limit the data but cannot reshape the output result.
          – Anthony Winzlet
          Nov 21 '18 at 19:17












          Ah great, good to know! Thanks for your help
          – philip yoo
          Nov 21 '18 at 19:21




          Ah great, good to know! Thanks for your help
          – philip yoo
          Nov 21 '18 at 19:21













          0














          use select function and define your required field to projection.



          const m = await Model.find({
          '_id': {
          $nin: [Ace, Bay],
          },
          'value.someCategory': {
          $type: 'object',
          },
          }).select('-_id value.someCategory').lean();

          console.log(m)





          share|improve this answer





















          • Sorry, I'm not using mongoose but thanks for the answer/reply
            – philip yoo
            Nov 21 '18 at 19:30
















          0














          use select function and define your required field to projection.



          const m = await Model.find({
          '_id': {
          $nin: [Ace, Bay],
          },
          'value.someCategory': {
          $type: 'object',
          },
          }).select('-_id value.someCategory').lean();

          console.log(m)





          share|improve this answer





















          • Sorry, I'm not using mongoose but thanks for the answer/reply
            – philip yoo
            Nov 21 '18 at 19:30














          0












          0








          0






          use select function and define your required field to projection.



          const m = await Model.find({
          '_id': {
          $nin: [Ace, Bay],
          },
          'value.someCategory': {
          $type: 'object',
          },
          }).select('-_id value.someCategory').lean();

          console.log(m)





          share|improve this answer












          use select function and define your required field to projection.



          const m = await Model.find({
          '_id': {
          $nin: [Ace, Bay],
          },
          'value.someCategory': {
          $type: 'object',
          },
          }).select('-_id value.someCategory').lean();

          console.log(m)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 19:20









          Raunik SinghRaunik Singh

          844




          844












          • Sorry, I'm not using mongoose but thanks for the answer/reply
            – philip yoo
            Nov 21 '18 at 19:30


















          • Sorry, I'm not using mongoose but thanks for the answer/reply
            – philip yoo
            Nov 21 '18 at 19:30
















          Sorry, I'm not using mongoose but thanks for the answer/reply
          – philip yoo
          Nov 21 '18 at 19:30




          Sorry, I'm not using mongoose but thanks for the answer/reply
          – philip yoo
          Nov 21 '18 at 19:30



          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'