我的gcloud数据存储区中有这种结构
"data": {
"obj": {
"added_time": "new Date().toUTCString()",
"person_id": 13,
"places": [{
"place_id": 12,
"place_name": "First Place",
"place_type": "1",
"fruits": [{
"id": "1",
"name": "orange"
},
{
"id": "2",
"name": "apple"
}
]
},
{
"place_id": 15,
"place_name": "secondPlace",
"place_type": "1",
"fruits": [{
"id": "1",
"name": "Grapes"
},
{
"id": "2",
"name": "Banana"
}
]
}
]
}
}
};
我想查询add_time,person_id和place_id
这是我的综合索引
indexes:
- kind: persons
properties:
- name: obj.person_id
- name: obj.added_time
- name: obj.places.place_id
我尝试了此查询
const query = datastore
.createQuery("persons")
.filter("obj.person_id", "=", 13)
.filter("obj.places.place_id", "=", 12);
这是可行的,但它也为我提供了所有其他地方。 例如,在此查询中,我应该只获得带有place_id 12的place对象,但是我得到另一个与place_id 13不应该出现的对象。
1)我怎么只能得到一个物体?
2)是否可以在数据存储区中使用“ in”运算符,例如places.place_id in (12,13)
谢谢。
答案 0 :(得分:2)
您要对persons
类型进行(简单)查询,因此结果将是persons
个实体,其中包括整个places
数组。
获取persons
实体仅在places
数组中位于单个位置的唯一方法是执行projection query。从技术上讲,您将获得一个由此类persons
实体组成的数组-每个匹配位置对应一个实体,但是由于您也要按place_id
进行过滤,因此该数组将仅包含一个元素。
您将需要特别注意,因为您还将投影数组属性,请参见Projections and array-valued properties。并检查与投影有关的Restrictions on queries。
否,过滤不支持IN
运算符,请参见gcloud datastore: Can I filter with IN or Contains operator?