删除查询中的对象 ID [MongoDB]

Fri*_*cia 0 mongodb mongodb-query

目前我正在编写一个查询,该查询显示具有特定注册号的卡车的信息,但不允许在我的查询中显示对象 ID。到目前为止,我能够获得所需的输出,但尽管在查询中输入了“_id”:0,但在查询期间对象 ID 仍然存在。

[询问]

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

[输出]

{
    "_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"
    }
}
Run Code Online (Sandbox Code Playgroud)

vis*_*hnu 5

您应该将projection作为第二个参数传递给该find方法。在您的情况下,您可以_id通过提供投影来抑制结果中的字段,如下所示

db.collection.find({
  $or: [
    {
      "TRUCK.registration": "PKR768"
    },
    {
      "TRUCK.registration": "PKR008"
    },
    {
      "TRUCK.registration": "SST005"
    }
  ]
},
{
  _id: 0
})
Run Code Online (Sandbox Code Playgroud)