我在collection1參考檔案中有僵尸檔案,這些檔案已從collection2
const collection1 = new Schema({
title: { type: String },
date: { type: Date, default: Date.now },
ref: { type: Schema.Types.ObjectId, ref: 'collection2' }
})
如何遍歷所有檔案collection1并洗掉refs其中不存在 ObjectId 的任何檔案collection2?
uj5u.com熱心網友回復:
聚合解決方案將是理想的,但以下內容適用于我的用例:
const removeZombies = async () => {
const col1 = await collection1.find({})
col1.forEach(async item => {
const colRef = await collection2.findById(item.ref)
if (colRef === null) {
collection1.deleteOne({ _id: item._id })
.then(doc => console.log(doc))
}
})
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/375264.html
下一篇:在貓鼬中洗掉評論父母
