簡體   English   中英

使用 Mongo ObjectId 作為索引

[英]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;
}, {});

listIdnew 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM