Collection.remove() 函數用於移除 Collection 中的文件,類似於 SQL 資料庫的 DELETE 陳述式。它採用搜尋條件字串 (SearchConditionStr) 作為參數,指定應從 Collection 中移除的文件 (關於 SearchConditionStr 的詳細說明,請參閱 第 4.3.2 節,「Collection.find()」)。如果未提供搜尋條件字串,或提供空字串,remove() 會傳回錯誤。如果傳遞任何評估為 true 但不符合任何文件的運算式 (例如,「true」或「_id IS NOT NULL」) 作為搜尋條件字串,則會移除 Collection 中的所有文件。
limit(:將要刪除的文件數量限制為int)。int-
sort(:根據sortCriteriaList)sortCriteriaList對要刪除的文件排序,sortCriteriaList是逗號分隔的清單或的陣列。每個sortCriteria都包含一個元件名稱和搜尋順序 (遞增為sortCriteriaasc,遞減為desc)。例如sort('name asc', 'age desc')sort(['name asc', 'age desc'])
此方法通常與
limit()方法結合使用,以決定要刪除符合搜尋條件字串的哪些文件。
也支援使用 bind() 進行參數繫結,execute() 函數會觸發 remove 操作的實際執行。以下範例顯示如何使用 Collection.remove() 函數。它假設已將一些文件新增至 Collection,如 第 4.3.1 節,「Collection.add()」中的程式碼範例所示。
// Use the collection 'my_collection'
var myColl = db.getCollection('my_collection');
// Remove documents by criteria
myColl.remove('name like :name AND age < :age').
limit(1).bind('name','N%').bind('age', 60).execute();
另請參閱 CollectionRemoveFunction,了解 add() 在 EBNF 中的語法。