我試圖查詢兩個用戶之間的現有對話,因此如果他們已經有一個對話,他們不會在資料庫中創建多個對話。
我無法通過此查詢找到這兩個用戶之間已經存在的對話并嘗試使用 $all 運算子。
我之前可以通過省略“參與者”欄位來查詢對話,因此認為它也適用于此查詢,但事實并非如此。
我需要找到與 BOTH senderId 和 recID 的對話
我究竟做錯了什么?
謝謝!
我試過的查詢(不作業)
// 1
const existingConvo = await Conversation.findOne({
userId: { $all: [newMsg.senderId, newMsg.recId] },
})
// 2
const existingConvo = await Conversation.findOne({
userId: [newMsg.senderId, newMsg.recId],
})
模型
const conversationSchema = mongoose.Schema(
{
participants: [participantSchema],
}
)
const participantSchema = mongoose.Schema(
{
userId: {
type: mongoose.Schema.Types.ObjectId,
required: true, // false because we can generate notifications
ref: `User`,
},
username: {
type: String,
required: true,
},
profileUrl: {
type: String,
required: true,
},
},
{ _id: false }
)
uj5u.com熱心網友回復:
看起來您可能正在尋找這種語法:
{ $and: [ { userId: newMsg.senderId }, { userId: newMsg.recId }] }
這是來自MongoDB 檔案的更詳細資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/397018.html
標籤:javascript MongoDB 猫鼬
下一篇:"message":"errorMongooseError:操作`userinfos.insertOne()`緩沖在10000毫秒后超時"
