Mặc định MongoDB luôn trả về trường _id. Để ẩn, đặt _id: 0 trong projection (đối số thứ 2 của find()). Tạo collection:
> db.demo575.insertOne({id:101,Information:{Name:"Chris",Age:21}});
{ "acknowledged" : true, "insertedId" : ObjectId("5e916a55581e9acd78b427f7") }
> db.demo575.insertOne({id:102,Information:{Name:"David",Age:20}});
{ "acknowledged" : true, "insertedId" : ObjectId("5e916a5f581e9acd78b427f8") }
> db.demo575.insertOne({id:101,Information:{Name:"Bob",Age:23}});
{ "acknowledged" : true, "insertedId" : ObjectId("5e916a67581e9acd78b427f9") }
Hiển thị đầy đủ (có _id):
> db.demo575.find();
{ "_id" : ObjectId("5e916a55581e9acd78b427f7"), "id" : 101, "Information" : { "Name" : "Chris", "Age" : 21 } }
{ "_id" : ObjectId("5e916a5f581e9acd78b427f8"), "id" : 102, "Information" : { "Name" : "David", "Age" : 20 } }
{ "_id" : ObjectId("5e916a67581e9acd78b427f9"), "id" : 101, "Information" : { "Name" : "Bob", "Age" : 23 } }
Truy vấn id=101, chỉ hiển thị Information.Name (ẩn _id, ẩn id, ẩn Age):
> db.demo575.find({id:101},{"Information.Name":1});
Kết quả (vẫn có _id - MongoDB luôn trả _id trừ khi tắt tường minh):
{ "_id" : ObjectId("5e916a55581e9acd78b427f7"), "Information" : { "Name" : "Chris" } }
{ "_id" : ObjectId("5e916a67581e9acd78b427f9"), "Information" : { "Name" : "Bob" } }
Để ẩn hoàn toàn _id: db.demo575.find({id:101},{"Information.Name":1, _id:0})
Quy tắc projection: Không thể kết hợp include (1) và exclude (0) trừ _id. Nếu include bất kỳ trường nào, các trường khác tự động exclude (trừ _id).