首页 > 解决方案 > 无效的查询。您不能将“array_contains”过滤器与“not_in”过滤器一起使用

问题描述

我有一个名为频道的集合。

用户被添加到userIds数组中,记录也被保存到名为serverId的字段中。

我只需要一个新创建或更新的行列表来更新到我的本地房间数据库。

每次发生任何更改时,我都会更新snapshotTime字段。

以前我使用以下代码,其中lastTime 是本地房间 db 的 max(snapshotTime)

FirestoreUtil.chatChannelCollectionRef
    .whereArrayContains("userIds", currentUserId)
    //.whereGreaterThan("snapshotTime", lastTime)
    .orderBy("snapshotTime", Query.Direction.ASCENDING)
    .startAfter(lastTime)
    .addSnapshotListener (activity) { snapshots, e ->
        if (e != null) {
            Log.e("exception occurred", "")
            return@addSnapshotListener
        }

        for (dc in snapshots!!.documentChanges) {
            when (dc.type) {
                DocumentChange.Type.ADDED -> {
                    val chatChannel = dc.document.toObject(ChatChannel::class.java)
                    chatChannel.serverId = dc.document.id
                    repository.insertOrUpdateChannel(channelDao, chatChannel)
                    Log.e(TAG, "ADDED chatChannel: ${dc.document.data} - ${dc.document.id}")
                }
                DocumentChange.Type.MODIFIED -> {
                    val chatChannel = dc.document.toObject(ChatChannel::class.java)
                    chatChannel.serverId = dc.document.id
                    repository.insertOrUpdateChannel(channelDao, chatChannel)
                    Log.e(TAG, "MODIFIED chatChannel: ${dc.document.data} - ${dc.document.id}")
                }
                DocumentChange.Type.REMOVED -> {
                    val chatChannel = dc.document.toObject(ChatChannel::class.java)
                    chatChannel.serverId = dc.document.id
                    //MessageDatabase.getDatabase(AjoowebApplication.applicationContext()).runInTransaction {
                    repository.deleteChannel(channelDao, chatChannel)
                    Log.e(TAG, "REMOVED chatChannel: ${dc.document.data} - ${dc.document.id}")
                }
                else -> {
                    Log.e(TAG, "MISSING: ${dc.document.data} - ${dc.document.id}")
                }
            }
        }
    }

它返回更新的字段,但不返回新记录。

所以我尝试了这个查询来只获取新行:

这里的 channels 是一个列表,其中包含本地数据库中的所有 channelsId

FirestoreUtil.firestoreInstance
    .collection(FBCollections.CHANNEL_COL)
    .whereNotIn("serverId", channels)
    .whereArrayContains("userIds", currentUserId)
    .get()
    .addOnSuccessListener (activity) { snapshots ->
        for (dc in snapshots!!.documents) {
            val chatChannel = dc.toObject(ChatChannel::class.java)!!
            chatChannel.serverId = dc.id
            repository.insertOrUpdateChannel(channelDao, chatChannel)
            Log.e(TAG, "LOADED chatChannel: ${dc.data} - ${dc.id}")
        }
    }

现在遇到的错误是:

无效的查询。您不能将“array_contains”过滤器与“not_in”过滤器一起使用

我怎样才能得到新创建的记录呢?

标签: androidfirebasekotlingoogle-cloud-firestore

解决方案


推荐阅读