37

I have started to use mongodb just a day back and have encountered an issue. I searched on net and stackoverflow for how to hide _id value in final answer and following the answers provided I tried to get my code run but still the _id part shows.

P.S.: I am using cloud9 as the ide.

var mongo = require('mongodb').MongoClient;
mongo.connect('mongodb://localhost:27017/learnyoumongo', function(err, database) {
        if(err) throw err;
        const db = database.db('learnyoumongo');
        var parrots = db.collection('parrots');
        parrots.find({
            age: { $gt: +process.argv[2] }
        },{
            name: 1,
            age: 1,
            _id: 0
        }).toArray(function(err, docs){
            if(err) throw err;
            console.log(docs);
            database.close();
        });
});
Zoe
  • 27,060
  • 21
  • 118
  • 148
snehal maheshwari
  • 472
  • 1
  • 4
  • 8
  • Could you please post the structure of the document?? – Alejandro Montilla Jan 25 '18 at 20:28
  • You can't really have both 1s and 0s at the same time. Thus, you either choose what you want by marking fields with 1, or choose what you don't want by marking fields with 0. E.g. I have 4 fields in an object `Email, Name, Age, Gender`. I wanna get only `Email` and `Name`. I could either do `{ Email: 1, Name: 1}` or `{ Age: 0, Gender: 0}` – oneturkmen Jan 25 '18 at 20:29
  • Your code is fine, I've tested and it works. Do you have a error?? It not returning the data as expected?? – Alejandro Montilla Jan 25 '18 at 20:44
  • 1
    Yea, I get that 0 and 1 part but my code does not seem to understand i. – snehal maheshwari Jan 26 '18 at 03:12
  • no, I don't have any error, but the output is not what I require.I want to hide id property which try to do using the 0 flag but still id is shown in the final output – snehal maheshwari Jan 26 '18 at 03:14

1 Answers1

99

you could separate projection like this:

    parrots.find({
        age: { $gt: +process.argv[2] }
    }).project({_id:0}).toArray(function(err, docs){
        if(err) throw err;
        console.log(docs);
        database.close();
    });

i had the same issue with being unable to get projection to work, and the above method worked for me

Jo Gro
  • 1,088
  • 6
  • 5
  • 1
    Thanks!! That worked. Would love to know why it doesn't work as explained in the docs. – Ben Lamm Mar 09 '18 at 02:39
  • 18
    This is a big breaking change; thanks for pointing it out. After seeing this answer, I poked around the github repo. It's mentioned there, with "v2 versus v3" examples. (Lot of breaking changes at v3). https://github.com/mongodb/node-mongodb-native/blob/master/CHANGES_3.0.0.md#find – R.L. Brown Apr 18 '18 at 14:21
  • 1
    Great answer ! Spot On !!! – zulkarnain shah Aug 08 '18 at 04:27
  • 1
    Thats really sucks! I spent an hour wondering whats wrong, my query works fine in Robo 3T and it doesn't work in node! : / Thanks for your answer! – Deano Apr 04 '19 at 00:08
  • Thanks! It works! I spent hours scratching my head until I found this post. – Oliver Zhang Jul 29 '20 at 07:20
  • Excellent!! Thanks a lot. I was wondering why my query is not working. Thanks again! – Tapashee Tabassum Urmi Oct 14 '20 at 05:57
  • I'm new to mongo too and this problem made me realize that the methods for mongosh and the node.js driver are not the same. the node.js driver has the projection() function to filter out results. This seems needless. But good to know.https://docs.mongodb.com/drivers/node/current/fundamentals/crud/read-operations/project/ – muchavie Oct 08 '21 at 00:30