在couchbase数据库中,我们有一个带有相对较大对象的桶。这些物体里面还有其他物体,比如说人。这应该是一个数组,但是出于某些原因,我们不得不将其创建为对象对象,这就是它的外观:
{
"companyName": "company name",
"companyid": "11111-GUID-11111",
"people": {
"22222-GUID-22222": {
"peopleid": "22222-GUID-22222",
"name": "name1"
},
"33333-GUID-33333": {
"peopleid": "33333-GUID-33333",
"name": "name2"
},
"44444-GUID-44444": {
"peopleid": "44444-GUID-44444",
"name": "name3"
}
}
}
使用此结构,我可以创建一个查询,以便使用如下查询获取“people”对象:
SELECT c.*
FROM companies c
WHERE ANY v IN OBJECT_VALUES(c.people) SATISFIES v.peopleid = "22222-GUID-22222" END;
这种结构的问题是数据库使用整个company对象进行响应,但是我只需要满足条件的“people”对象,而不需要具有
"22222-GUID-22222"
以外的id的人。有没有办法用Couchbase来实现这个目的?
最佳答案
SELECT FIRST v FOR v IN OBJECT_VALUES(c.people) WHEN v.peopleid = "22222-GUID-22222" END AS people
FROM companies c
WHERE ANY v IN OBJECT_VALUES(c.people) SATISFIES v.peopleid = "22222-GUID-22222" END;