[英]How to update a nested array field inside another nested array in mongo DB using Mongoose Node JS
[英]Node - Mongo DB - How do I query for a specific nested field
簡而言之:我使用 mongodb 為我的 node-express 應用程序設置了會話身份驗證。 如果用戶在數據庫中已經有一個有效的會話實體,則不應創建新會話。 現在我想在我的會話集合中查詢特定的用戶 ID。
該集合如下所示:
{"_id":"HbAE-qMXPWl7C8tDUnC****q8OoEf76vN","expires":{"$date":{"$numberLong":"1576133617190"}},"session":"{\"cookie\":{\"originalMaxAge\":86399999,\"expires\":\"2019-12-12T06:36:29.898Z\",\"secure\":false,\"httpOnly\":true,\"path\":\"/\",\"sameSite\":true},\"user\":{\"uid\":\"5df08a1b8a60c174840d2986\"}}"}
我的查詢如下所示:
public async compareSessionIds(uid: string, sid: string) {
//uid is: 5df08a1b8a60c174840d2986 ==> the same as in the entity
const user: User | null = await this.connectionManager.db('users').collection<User>('sessions').findOne({uid: uid} as any);
console.log('user found: ', user); // user found: null
return user && user._id === sid;
}
但是我的查詢每次都返回null
所以我假設我的查詢是錯誤的。 如何正確查詢 mongodb 中的特定嵌套字段?
你的查詢應該是這樣的:
.findOne({"session.user.uid": uid});
因為, uid
在session.user
! 希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.