1. 程式人生 > 資料庫 >mongoDB使用投影剔除‘額外’欄位的操作過程

mongoDB使用投影剔除‘額外’欄位的操作過程

簡介

實際開發過程中,為便於開發人員定位問題,常存在多個額外的欄位。例如:增加createdAt、updatedAt欄位以檢視資料的建立和更改時間。而對於客戶端而言,無需知道其存在。針對以上情況,本文詳細介紹了“額外”欄位的用途以及處理過程。

技術棧

  • mongodb 4.0.20
  • mongoose 5.10.7

1 "額外"欄位是什麼

1.1 "額外"是指與業務無關

mongodb中,collection中儲存的欄位並不僅僅有業務欄位。有些情況下,會儲存多餘的欄位,以便於開發人員定位問題、擴充套件集合等。

額外的含義是指 和業務無關、和開發相關的欄位。這些欄位不需要被使用者所瞭解,但是在開發過程中是至關重要的。

1.2 產生原因

產生額外欄位的原因是多種多樣的。

  • 如使用mongoose外掛向db中插入資料時,會預設的生成_id、__v欄位
  • 如軟刪除,則是通過控制is_deleted實現..

2 額外欄位的分類

額外欄位的產生原因有很多,可以以此進行分類。

2.1 _id、__v欄位

產生原因:以mongoose為例,通過schema->model->entity向mongodb中插入資料時,該資料會預設的增加_id、__v欄位。

_id欄位是由mongodb預設生成的,用於文件的唯一索引。型別是ObjectID。mongoDB文件定義如下:


MongoDB creates a unique index on the _id field during the creation of a collection. The _id index prevents clients from inserting two documents with the same value for the _id field. You cannot drop this index on the _id field.<

__v欄位是由mongoose首次建立時預設生成,表示該條doc的內部版本號。


The versionKey is a property set on each document when first created by Mongoose. This keys value contains the internal revision of the document. The versionKey option is a string that represents the path to use for versioning. The default is __v.

2.2 createdAt、updatedAt欄位

createdAt、updatedAt欄位是通過timestamp選項指定的,型別為Date。


The timestamps option tells mongoose to assign createdAt and updatedAt fields to your schema. The type assigned is Date.By default,the names of the fields are createdAt and updatedAt. Customize the field names by setting timestamps.createdAt and timestamps.updatedAt.

2.3 is_deleted欄位

is_deleted欄位是實現軟刪除一種常用的方式。在實際業務中,出於各種原因(如刪除後用戶要求再次恢復等),往往採用的軟刪除,而非物理刪除。

因此,is_deleted欄位儲存當前doc的狀態。is_deleted欄位為true時,表示當前記錄有效。is_deleted欄位為false時,表示當前記錄已被刪除。

3 額外欄位相關操作

3.1 額外欄位生成

_id欄位是必選項;__v、createdAt、updatedAt欄位是可配置的;status欄位直接加在s對應的chema中。相關的schema程式碼如下:

isdeleted: {
 type: String,default:true,enum: [true,false],},id: {
 type: String,index: true,unqiue: true,default:uuid.v4(),}},{timestamps:{createdAt:'docCreatedAt',updatedAt:"docUpdatedAt"},versionKey:false});

通過配置schema中的timestamps選項,可以將createdAt和updatedAt欄位加入到doc中。在建立和更新doc時,這兩個欄位無需傳入,就會自動變化。

3.2 額外欄位清理

通過3.1可以明確的產生若干額外欄位,但是客戶端呼叫介面並返回時,這些欄位是無需得知的。因此需對額外欄位進行清理。清理方式分為投影和過濾。

以query、update介面為例。其中query介面用於:1、查詢指定欄位 2、查詢全部欄位 3、分頁排序查詢。update介面用於更新並返回更新後的資料。

根據是否需要指定欄位、和collection中有無內嵌的情況劃分,一共有4類。接著針對這4種情況進行分析。

1、有指定欄位、無內嵌

2、無指定欄位、無內嵌

3、有指定欄位、有內嵌

4、無指定欄位、有內嵌

3.2.1 投影

有指定欄位是指在查詢時指定查詢欄位,而無需全部返回。mongo中實現指定的方式是投影 (project) 。mongo官方文件中定義如下:


The $project takes a document that can specify the inclusion of fields,the suppression of the _id field,the addition of new fields,and the resetting of the values of existing fields. Alternatively,you may specify the exclusion of fields.

$project可以做3件事:

1.指定包含的欄位、禁止_id欄位、新增新欄位

2.重置已存在欄位的值

3.指定排除的欄位

我們只需關注事情1、事情3。接著檢視mongoose中對project的說明:


When using string syntax,prefixing a path with - will flag that path as excluded. When a path does not have the - prefix,it is included. Lastly,if a path is prefixed with +,it forces inclusion of the path,which is useful for paths excluded at the schema level.

A projection must be either inclusive or exclusive. In other words,you must either list the fields to include (which excludes all others),or list the fields to exclude (which implies all other fields are included). The _id field is the only exception because MongoDB includes it by default.

注意:此處指query "projection"

mongoose表明:投影要麼是全包含,要麼是全剔除。不允許包含和剔除同時存在。但由於

_id是MongoDB預設包含的,因此_id是個例外。

select project投影語句組裝程式碼:

 /**
 * 新增通用篩選條件
 * @param {*} stat 已裝配的篩選語句 
 * @param {*} collection collectionName
 * @return {*} 組裝完成的語句
 */
 function addCommonSelectCond(stat,collection)
 {
 if(typeof(stat)!="object") return;

 stat["_id"] = 0;
 stat["__v"] = 0;
 stat["status"] = 0;
 stat["docCreatedAt"] = 0;
 stat["docUpdatedAt"] = 0;

 var embeddedRes = hasEmbedded(collection);
 if(embeddedRes["isEmbedded"])
 {
 for(var item of embeddedRes["embeddedSchema"])
 {
 stat[item+"._id"] = 0;
 stat[item+".__v"] = 0;
 stat[item+".status"] = 0;
 stat[item+".docCreatedAt"] = 0; 
 stat[item+".docUpdatedAt"] = 0; 
 }
 }
 return stat;
 }

3.2.2 過濾

通過findOneAndupdate、insert、query等返回的doc物件中(已經過lean或者toObject處理),是資料庫中真實狀態。因此需要對產生的doc進行過濾,包括doc過濾和內嵌文件過濾。

/**
 * 處理自身及內嵌的表
 * @param {*} collection 查詢的表
 * @param {*} doc 已查詢出的結果
 * @returns doc 清理後的結果
 */
static clearDoc(collection,doc){
 if(doc === undefined || doc === null || typeof doc != "object" ) return null;
 doc = this.clearExtraField(doc);

 var res = hasEmbedded(collection);
 if(res["isEmbedded"])
 {
 let arr = res["embeddedSchema"];
 for(var item of arr){
 if(doc[item])
 doc[item] = this.clearArray(doc[item]);
 }
 }
 return doc;
}

static clearExtraField(doc){
 if(doc === null || typeof doc != "object")
 return;
 
 var del = delete doc["docCreatedAt"]&&
 delete doc["docUpdatedAt"]&&
 delete doc["_id"]&&
 delete doc["__v"]&&
 delete doc["status"];
 if(!del) return new Error("刪除額外欄位出錯");

 return doc;
}

static clearArray(arr)
{
 if(!Array.isArray(arr)) return;

 var clearRes = new Array();
 for(var item of arr){
 clearRes.push(this.clearExtraField(item));
 }
 return clearRes;
}

細心的讀者已經發現了,投影和過濾的欄位內容都是額外欄位。那什麼情況下使用投影,什麼情況下使用過濾呢?

關於這個問題,筆者的建議是如果不能確保額外欄位被剔除掉,那就採取雙重認證:查詢前使用投影,查詢後使用過濾。

4 總結

本文介紹了實際業務中往往會產生額外欄位。而在mongoDB中,"消除"額外欄位的手段主要是投影、過濾。

以使用頻率最高的查詢介面為例,整理如下:

指定選項 內嵌選項 查詢前投影 查詢後過濾
有指定 無內嵌 ×
有指定 有內嵌 ×
無指定 無內嵌 ×
無指定 有內嵌 ×

因此,筆者建議無論schema中是否配置了options,在查詢時組裝投影語句,查詢後進行結果過濾。這樣保證萬無一失,

額外欄位才不會漏到客戶端**。

到此這篇關於mongoDB使用投影剔除‘額外'欄位的文章就介紹到這了,更多相關mongoDB用投影剔除額外欄位內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!