Search code examples
node.jsmongodbmongodb-querynodejs-streamnodejs-server

How to select a single field for documents in a MongoDB collection?


I tried get(selected values) query in mongodb.that query in working fine in mongodb shell .but I tried to run node js it not working it showing all data.how to fix it.

query

db.collection('olc_prod_db_category').find({name: { $nin:['DISCONTINUE', 'LIQUOR MINI']}},{ "_id": 0}).toArray()

Expected ouput:

{ "id" : 3, "name" : "IRISH WHISKEY", "hasSubCategory" : "false", "parentId" : "30" }  

but I got this output:

{
    "_id": "5b4efd6fd53be829188070ca",
    "id": 3,
    "name": "IRISH WHISKEY",
    "hasSubCategory": "false",
    "parentId": "30"
}

Solution

  • Use .project cursor method

    db.collection('olc_prod_db_category').find(
      { name: { $nin:['DISCONTINUE', 'LIQUOR MINI']}}
    ).project({ _id: 0 }).toArray()