{
"_id": {
"$oid": "5f8f067ecf44854d4c3b5286"
},
"photo": "",
"followers": [
{
"$oid": "5f8f26f8cf44854d4c3b528f"
},
{
"$oid": "5f9f1984cf44854d4c3b5292"
},
{
"$oid": "5f8f06c9cf44854d4c3b528e"
},
{
"$oid": "604b1d0649a2052e912f9c3f"
},
{
"$oid": "5f8f0688cf44854d4c3b5287"
},
{
"$oid": "62371c3b8fabe53cf4386b1f"
}
],
"following": [
{
"$oid": "5f9f1984cf44854d4c3b5292"
},
{
"$oid": "5f8f26f8cf44854d4c3b528f"
},
{
"$oid": "5f8f06c9cf44854d4c3b528e"
}
],
"email": "[email protected]",
"firstname": "John",
"lastname": "Doe",
"username": "JohnDoe01",
"__v": 0
}
我正在嘗試從特定用戶那里獲取關注者串列。我有一個被阻止用戶的串列,所以我需要從關注者串列中排除這些用戶。例如:
let blockedUsers = ['5f8f26f8cf44854d4c3b528f', '5f9f1984cf44854d4c3b5292'];
僅供參考:關注者也是用戶物件
讓我知道需要的任何進一步細節。提前致謝。
uj5u.com熱心網友回復:
您可以$filter用來過濾不在阻止串列中的關注者(id)。
在執行比較時,請確保:
選項 1:阻止串列中的值轉換為ObjectId
要么
選項 2:投射follower到string
必須比較具有相同型別的值。
db.collection.aggregate({
$set: {
followers: {
$filter: {
input: "$followers",
cond: {
$not: {
$in: [
{
$toString: "$$this" // Or "$$this._id" for user Object
},
[
"5f8f26f8cf44854d4c3b528f",
"5f9f1984cf44854d4c3b5292"
]
]
}
}
}
}
}
})
示例 Mongo Playground
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/456255.html
