0

Am currently writing a query that displays info on trucks with specific registration numbers but am not allowed to show the object id within my query. So far I am able to get the desired output but despite putting "_id":0 in my query, the object ID is still present during my query.

[Query]

db.transport.find(
    {
        $or:[
            {"TRUCK.registration":"PKR768"},
            {"TRUCK.registration":"PKR008"},
            {"TRUCK.registration":"SST005"},
            {"_id":0},
        ]
    }
).pretty()

[Output]

{
    "_id" : ObjectId("5fab387f48a75b7c08cedfe4"),
    "TRUCK" : {
        "registration" : "PKR008",
        "capacity" : "22000",
        "weight" : "8800",
        "status" : "AVAILABLE"
    }
}
{
    "_id" : ObjectId("5fab387f48a75b7c08cedfe5"),
    "TRUCK" : {
        "registration" : "PKR768",
        "capacity" : "1234",
        "weight" : "3000",
        "status" : "AVAILABLE"
    }
}
{
    "_id" : ObjectId("5fab387f48a75b7c08cedfe7"),
    "TRUCK" : {
        "registration" : "SST005",
        "capacity" : "12000",
        "weight" : "50000",
        "status" : "USED"
    }
}

1 Answers1

5

You should pass the projection as a second parameter to the find method. In your case, you can suppress the _id field from the results by providing the projection as shown below

db.collection.find({
  $or: [
    {
      "TRUCK.registration": "PKR768"
    },
    {
      "TRUCK.registration": "PKR008"
    },
    {
      "TRUCK.registration": "SST005"
    }
  ]
},
{
  _id: 0
})
vishnu
  • 1,961
  • 2
  • 7
  • 11