久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

FireStore - 如何繞過數組“不包含"查詢

FireStore - how to get around array quot;does-not-containquot; queries(FireStore - 如何繞過數組“不包含查詢)
本文介紹了FireStore - 如何繞過數組“不包含"查詢的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

經過一些研究,很明顯我不能使用 FireStore 來查詢給定數組不包含的項目.有人對此用例有解決方法嗎?...

After some research, it's seems clear that I cannot use FireStore to query items a given array does NOT contain. Does anyone have a workaround for this use case?...

用戶注冊后,應用會獲取一堆卡片,每張卡片在 FireStore 中都有對應的卡片"文檔.用戶與卡片交互后,卡片文檔將用戶的 uid 添加到字段數組(例如:usersWhoHaveSeenThisCard:[userUID]),用戶"文檔將卡片的 uid 添加到字段數組(例如:cardThisUserHasSeen:[cardUID]).用戶"文檔存在于用戶"集合中,卡片"文檔存在于卡片"集合中.

After a user signs up, the app fetches a bunch of cards that each have a corresponding "card" document in FireStore. After a user interacts with a card, the card document adds the user's uid to a field array (ex: usersWhoHaveSeenThisCard: [userUID]) and the "user" document adds the card's uid to a field array (ex: cardsThisUserHasSeen: [cardUID]). The "user" documents live in a "user" collection and the "card" documents live in a "card" collection.

目前,我想獲取用戶未與之交互的所有卡片.但是,這是有問題的,因為我只知道用戶與之交互的卡片,因此 .whereField(usersWhoHaveSeenThisCard, arrayContains: currentUserUID) 將不起作用,因為我需要一個不存在的arrayDoesNotContain"語句.

Currently, I'd like to fetch all cards that a user has NOT interacted with. However, this is problematic, as I only know the cards that a user has interacted with, so a .whereField(usersWhoHaveSeenThisCard, arrayContains: currentUserUID) will not work, as I'd need an "arrayDoesNotContain" statement, which does not exist.

最后,用戶不能擁有卡片,因此我無法在卡片文檔中創建真/假布爾字段(例如:userHasSeenThisCard: false)并根據該條件進行搜索.

Finally, a user cannot own a card, so I cannot create a true / false boolian field in the card document (ex: userHasSeenThisCard: false) and search on that criteria.

我能想到的唯一解決方案是在卡片文檔上創建一個新的字段數組,其中包括所有沒有看到卡片的用戶(例如:usersWhoHaveNotSeenThisCard: [userUID]),但這意味著每個用戶注冊必須將他們的 uid 寫入 1000 多個卡片文檔,這會占用我的數據.

The only solution I can think of, would be to create a new field array on the card document that includes every user who has NOT seen a card (ex: usersWhoHaveNotSeenThisCard: [userUID]), but that means that every user who signs up would have to write their uid to 1000+ card documents, which would eat up my data.

我可能只是運氣不好,但我希望對 NOSQL/FireStore 更了解的人可以提供一些見解.

I might just be out of luck, but am hoping someone more knowledgeable with NOSQL / FireStore could provide some insight.

// If any code sample would help, please let me know and I'll update - I think this is largely conceptual as of now

推薦答案

有一個公認的很好的答案,但是,它不能直接解決這個問題,所以這里......(這可能會也可能不會有幫助,但確實有效)

There is an accepted and good answer, however, it doesn't provide a direct solution to the question so here goes... (this may or may not be helpful but it does work)

我不確切知道您的 Firestore 結構是什么,所以這是我的假設:

I don't know exactly what your Firestore structure is so here's my assumption:

cards
   card_id_0
      usersWhoHaveSeenThisCard
         0: uid_0
         1: uid_1
         2: uid_2
   card_id_1
      usersWhoHaveSeenThisCard
         0: uid_2
         1: uid_3
   card_id_2
      usersWhoHaveSeenThisCard
         0: uid_1
         1: uid_3

假設我們想知道 uid_2 沒有看到哪些卡 - 在本例中是 card_id_2

Suppose we want to know which cards uid_2 has not seen - which in this case is card_id_2

func findCardsUserHasNotSeen(uidToCheck: String, completion: @escaping ( ([String]) -> Void ) ) {
    let ref = self.db.collection("cards")

    ref.getDocuments(completion: { snapshot, err in
        if let err = err {
            print(err.localizedDescription)
            return
        }

        guard let docs = snapshot?.documents else {
            print("no docs")
            return
        }
        var documentsIdsThatDoNotContainThisUser = [String]()
        for doc in docs {
            let uidArray = doc.get("usersWhoHaveSeenThisCard") as! [String]
            let x = uidArray.contains(uidToCheck)
            if x == false {
                documentsIdsThatDoNotContainThisUser.append(doc.documentID)
            }
        }
        completion(documentsIdsThatDoNotContainThisUser)
    })
}

那么,這樣的用例

func checkUserAction() {
    let uid = "uid_2" //the user id to check
    self.findCardsUserHasNotSeen(uidToCheck: uid, completion: { result in
        if result.count == 0 {
            print("user: (uid) has seen all cards")
            return
        }
        for docId in result {
            print("user: (uid) has not seen: (docId)")
        }
    })
}

和輸出

user: uid_2 has not seen: card_id_2

此代碼遍歷文檔,獲取存儲在每個文檔 usersWhoHaveSeenThisCard 節點中的 uid 數組,并確定 uid 是否在數組中.如果沒有,它會將 documentID 添加到 documentsIdsThatDoNotContainThisUser 數組中.檢查完所有文檔后,將返回不包含用戶 ID 的文檔 ID 數組.

This code goes through the documents, gets the array of uid's stored within each documents usersWhoHaveSeenThisCard node and determines if the uid is in the array. If not, it adds that documentID to the documentsIdsThatDoNotContainThisUser array. Once all docs have been checked, the array of documentID's that do not contain the user id is returned.

知道 Firestore 的速度有多快,我針對一個大型數據集運行了代碼,結果很快就返回了,因此對于大多數用例來說它不會造成任何延遲.

Knowing how fast Firestore is, I ran the code against a large dataset and the results were returned very quickly so it should not cause any kind of lag for most use cases.

這篇關于FireStore - 如何繞過數組“不包含"查詢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

how to set scrollview content size in swift 3.0(如何在 swift 3.0 中設置滾動視圖內容大小)
Create partial-screen UIPageViewController programmatically(以編程方式創建部分屏幕 UIPageViewController)
UIImageView zoom and pinch in UIScrollView(UIImageView 在 UIScrollView 中縮放和捏合)
Make background color change during scroll(在滾動期間更改背景顏色)
How to set UIScrollView Height in Swift(如何在 Swift 中設置 UIScrollView 高度)
Disable UIPageViewController bouncing - Swift(禁用 UIPageViewController 彈跳 - Swift)
主站蜘蛛池模板: 国产一区91精品张津瑜 | 欧美一区视频 | 成人av电影网 | 精品亚洲第一 | 99久久久国产精品 | 国产激情视频在线 | 日本成人毛片 | 精品亚洲一区二区三区 | 亚洲精品在线视频 | 国产羞羞视频在线观看 | 在线观看国产www | 伊人在线 | 日韩精品一区二区三区老鸭窝 | 99久久婷婷国产综合精品电影 | 久久精品国产99国产精品 | 爱爱免费视频 | 国产精品视频一区二区三区不卡 | 国产精品久久久久久久 | 青青草一区 | 国产精品国产三级国产aⅴ中文 | 亚洲视频在线免费观看 | 成人免费网站www网站高清 | 欧美激情第一区 | 午夜影院在线观看视频 | 日本字幕在线观看 | 日韩看片| 国产一二区在线 | 欧美一区二区三区小说 | 亚洲精品福利在线 | 美女国产精品 | 国产成人福利在线 | 成人av在线播放 | av黄色免费在线观看 | 免费观看色 | 精品成人在线观看 | 精产嫩模国品一二三区 | 狠狠伊人| 国产精品久久久久一区二区三区 | 国产亚洲一区二区精品 | 艹逼网| 精品国产久 |