如何使用包含数组的数组包含映射 [英] How do I use array-contains for array containing map

查看:69
本文介绍了如何使用包含数组的数组包含映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Cloud Firestore中,我需要使用 array-contains 查询在包含地图的数组中查找匹配项.

In Cloud Firestore I am needing to find matches in an array that contains maps using the array-contains query.

我可以很容易地匹配直接字符串,数字等,但是当数组的值是映射时,它总是失败.文档没有暗示这是否可能.

I can very easily match direct strings, numbers etc however it always fails when the values of the array is a map. The documentation doesn't allude to is this is possible or not.

这是一些数据的例子

{
 data:
    [
        item_1: {...},
        item_2: {...}
    ]
}

这是查询的一个例子

db.collection("list")
  .where("data", "array-contains", "item_1")
  .get()
  .then(collectionRef => {
      //do something with collection
  })

我期望数组包含的内容与地图的最高值(item_x)匹配,但目前与此不匹配.我认为这是因为它想将地图的整个内容作为数组项的值"进行匹配.

I am expecting the array-contains to match the top value (item_x) of the map, at the moment it doesn't match this. I assume this is because it wants to match the entire content of the map as the "value" of the array item.

任何帮助将不胜感激.如果这不可能,我可能不得不重新考虑我的数据结构.对于上下文,我在云函数中使用它来在原始文档更新时更新数组中的项目(它们是对文档的引用).

Any help would be appreciated. I might have to re-think my data structure if this is not possible. For context I am using this in a cloud function to update items (they are a reference to a doc) in an array when their original doc is updated.

推荐答案

数组包含的查询仅与数组中的整个项目匹配.如果该项目是一个对象,则查询必须提供整个确切的对象,以匹配数组中的相等性.

array-contains queries only match the entire item in an array. If that item is an object, the query must provide the entire, exact object to match for equality in the array.

不支持您当前正在执行的操作.您将不得不在文档中提供其他字段以进行搜索,可能会重复该文档中的数据,这对于NoSQL类型数据库是常见的.

What you're doing right now isn't supported. You will have to provide some other field in the document to search against, possibly duplicating data in that document, which is common for NoSQL type databases.

这篇关于如何使用包含数组的数组包含映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆