![](/img/trans.png)
[英]Trying to find index of ObjectId in Mongo DB with indexOf
[英]Using Mongo ObjectId as an Index
我正在嘗試遍歷從Model.find()
調用中檢索到的數組。
const lists = ListModel.find();
const listSet = lists.reduce<Record<string, Set<string>>>((acc, list) => {
const listId = list._id;
acc[listId] = new Set()
return acc;
}, {});
listId
是new ObjectId(1234qwer1234qwer)
,錯誤是Type 'ObjectId' cannot be used as an index type.
處理這個問題的理想方法是什么? 不久之后,我可能會想在某個時候使用objectId
引用。
編輯:我曾嘗試使用list.id
而不是list._id
,但出現Type 'Buffer' cannot be used as an index type.
您可以使用._id.toString()
獲取 ID 的字符串化版本,您應該能夠將其用作索引。
您可以使用.id
而不是._id
來獲取 ID 的字符串化版本,您應該能夠將其用作索引。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.