隱藏 MongoDB 中的 ID 欄位


讓我們建立一個帶有文件的集合 -

> 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")
}

在集合中顯示所有文件,方法是使用 find() 方法 -

> 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 } }

以下是如何隱藏 id 欄位值並顯示其餘部分 -

> db.demo575.find({id:101},{"Information.Name":1});

這將產生以下輸出 -

{ "_id" : ObjectId("5e916a55581e9acd78b427f7"), "Information" : { "Name" : "Chris" } }
{ "_id" : ObjectId("5e916a67581e9acd78b427f9"), "Information" : { "Name" : "Bob" } }

更新於:2020 年 5 月 15 日

523 次瀏覽

開啟你的 職業

完成課程以獲得認證

開始
廣告
© . All rights reserved.