Map
MongooseMap.prototype.$isMongooseMap
MongooseMap.prototype.clear()
MongooseMap.prototype.delete()
MongooseMap.prototype.get()
MongooseMap.prototype.set()
MongooseMap.prototype.toBSON()
MongooseMap.prototype.toJSON()
MongooseMap.prototype.$isMongooseMap
類型(Type)
- «屬性»(«property»)
所有 Mongoose map 實例都設為 true
MongooseMap.prototype.clear()
覆寫原生 Map 的 clear()
函數以支援變更追蹤。
MongooseMap.prototype.delete()
覆寫原生 Map 的 delete()
函數以支援變更追蹤。
MongooseMap.prototype.get()
覆寫原生 Map 的 get()
函數以支援 Mongoose 的 getter。
MongooseMap.prototype.set()
覆寫原生 Map 的 set()
函數以支援 setter、populate()
和變更追蹤。請注意,Mongoose map *僅*支援字串和 ObjectIds 作為鍵。
鍵也不能
- 以特殊屬性
prototype
、constructor
和__proto__
命名 - 以錢字符號 (
$
) 開頭 - 包含任何點 (
.
)
範例
doc.myMap.set('test', 42); // works
doc.myMap.set({ obj: 42 }, 42); // Throws "Mongoose maps only support string keys"
doc.myMap.set(10, 42); // Throws "Mongoose maps only support string keys"
doc.myMap.set("$test", 42); // Throws "Mongoose maps do not support keys that start with "$", got "$test""
MongooseMap.prototype.toBSON()
將此 map 轉換為原生 JavaScript Map,以便 MongoDB 驅動程式可以序列化它。
MongooseMap.prototype.toJSON()
參數
[options]
«物件»(«Object»)[options.flattenMaps=false]
«布林值»(«Boolean») 設定為true
以將 map 轉換為 POJO 而不是原生 JavaScript map
將此 map 轉換為用於 JSON.stringify()
的原生 JavaScript Map。設定 flattenMaps
選項以將此 map 轉換為 POJO。
範例
doc.myMap.toJSON() instanceof Map; // true
doc.myMap.toJSON({ flattenMaps: true }) instanceof Map; // false