我有这样的文件
[
{
"id": "1",
"name": "My Product 1",
"variants": [
{
"id": 2179,
"code": "A",
"attributes": {
"att_set_1": {
"en": {
"name": "Attribute Set 1",
"data": [
{
"id": 919,
"title": "Height",
"label": "height_mm",
"v": 1200,
"unit": "mm"
},
{
"id": 921,
"title": "Weight",
"label": "weight",
"v": 500,
"unit": "kg"
},
{
"id": 923,
"title": "Other",
"label": "blah",
"v": 200,
"unit": "mm"
}
]
}
}
}
}
]
},
{
"id": "2",
"name": "My Product 2",
"variants": [
{
"id": 2180,
"code": "B",
"attributes": {
"att_set_1": {
"en": {
"name": "Attribute Set 1",
"data": [
{
"id": 919,
"title": "Height",
"label": "height_mm",
"v": 1300,
"unit": "mm"
},
{
"id": 921,
"title": "Weight",
"label": "weight",
"v": 200,
"unit": "kg"
},
{
"id": 923,
"title": "Other",
"label": "blah",
"v": 200,
"unit": "mm"
}
]
}
}
}
}
]
},
{
"id": "3",
"name": "My Product 3",
"variants": [
{
"id": 2181,
"code": "C",
"attributes": {
"att_set_1": {
"en": {
"name": "Attribute Set 1",
"data": [
{
"id": 919,
"title": "Height",
"label": "height_mm",
"v": 2000,
"unit": "mm"
},
{
"id": 921,
"title": "Weight",
"label": "weight",
"v": 999,
"unit": "kg"
},
{
"id": 923,
"title": "Other",
"label": "blah",
"v": 200,
"unit": "mm"
}
]
}
}
}
}
]
}
]
我现在想找到其中一个变体的高度> = 1200且重量> = 500
的所有产品在此示例中,这应该是我的产品1和我的产品3.我的产品2不匹配,因为重量属性低于标准。
我该怎么做?有办法吗?数据结构可以更改,但只有在真正需要时才能更改。
答案 0 :(得分:1)
我按照您的文档在我的cosmos db中创建了3个示例文档:
[
{
"id": "1",
"name": "My Product 1",
"variants": [
{
"id": 2179,
"code": "A",
"attributes": {
"att_set_1": {
"en": {
"name": "Attribute Set 1",
"data": [
{
"id": 919,
"title": "Height",
"label": "height_mm",
"v": 2330,
"unit": "mm"
},
{
"id": 921,
"title": "Weight",
"label": "weight",
"v": 2931,
"unit": "kg"
},
{
"id": 923,
"title": "Other",
"label": "blah",
"v": 200,
"unit": "mm"
}
]
}
}
}
}
]
},
{
"id": "2",
"name": "My Product 2",
"variants": [
{
"id": 2180,
"code": "B",
"attributes": {
"att_set_1": {
"en": {
"name": "Attribute Set 1",
"data": [
{
"id": 919,
"title": "Height",
"label": "height_mm",
"v": 100,
"unit": "mm"
},
{
"id": 921,
"title": "Weight",
"label": "weight",
"v": 200,
"unit": "kg"
},
{
"id": 923,
"title": "Other",
"label": "blah",
"v": 200,
"unit": "mm"
}
]
}
}
}
}
]
},
{
"id": "3",
"name": "My Product 3",
"variants": [
{
"id": 2181,
"code": "C",
"attributes": {
"att_set_1": {
"en": {
"name": "Attribute Set 1",
"data": [
{
"id": 919,
"title": "Height",
"label": "height_mm",
"v": 2000,
"unit": "mm"
},
{
"id": 921,
"title": "Weight",
"label": "weight",
"v": 999,
"unit": "kg"
},
{
"id": 923,
"title": "Other",
"label": "blah",
"v": 200,
"unit": "mm"
}
]
}
}
}
}
]
}
]
然后我使用SQL:
SELECT all FROM all join in all.variants join b in a.attributes.att_set_1.en.data where(b.title ='Height'和b.v> = 2000)或(b.title ='Weight'和b.v> = 1000)
结果数据:
[
{
"all": {
"id": "1",
"name": "My Product 1",
"variants": [
{
"id": 2179,
"code": "A",
"attributes": {
"att_set_1": {
"en": {
"name": "Attribute Set 1",
"data": [
{
"id": 919,
"title": "Height",
"label": "height_mm",
"v": 2330,
"unit": "mm"
},
{
"id": 921,
"title": "Weight",
"label": "weight",
"v": 2931,
"unit": "kg"
},
{
"id": 923,
"title": "Other",
"label": "blah",
"v": 200,
"unit": "mm"
}
]
}
}
}
}
]
}
},
{
"all": {
"id": "1",
"name": "My Product 1",
"variants": [
{
"id": 2179,
"code": "A",
"attributes": {
"att_set_1": {
"en": {
"name": "Attribute Set 1",
"data": [
{
"id": 919,
"title": "Height",
"label": "height_mm",
"v": 2330,
"unit": "mm"
},
{
"id": 921,
"title": "Weight",
"label": "weight",
"v": 2931,
"unit": "kg"
},
{
"id": 923,
"title": "Other",
"label": "blah",
"v": 200,
"unit": "mm"
}
]
}
}
}
}
]
},
{
"all": {
"id": "3",
"name": "My Product 3",
"variants": [
{
"id": 2181,
"code": "C",
"attributes": {
"att_set_1": {
"en": {
"name": "Attribute Set 1",
"data": [
{
"id": 919,
"title": "Height",
"label": "height_mm",
"v": 2000,
"unit": "mm"
},
{
"id": 921,
"title": "Weight",
"label": "weight",
"v": 999,
"unit": "kg"
},
{
"id": 923,
"title": "Other",
"label": "blah",
"v": 200,
"unit": "mm"
}
]
}
}
}
}
]
}
}
]
请注意,value
列是关键字,不能在文档中使用。所以,在我的文档中,我用v。
更新答案:
经过几次尝试后,似乎无法通过SQL语句直接从Cosmos DB查询所需的结果。
然而,当我们面对复杂的查询时,Cosmos DB为我们提供了存储过程。如果您对存储过程了解不多,可以阅读此article。
请参阅我创建的存储过程,如下所示:
function sample() {
var collection = getContext().getCollection();
var isAccepted = collection.queryDocuments(
collection.getSelfLink(),
'SELECT * FROM c',
function (err, feed, options) {
if (err) throw err;
var returnArray = [];
if (!feed || !feed.length){
getContext().getResponse().setBody('no docs found');
} else{
for(var i=0;i<feed.length;i++){
var foundHeight = false, foundWeight=false;
var dataArray = feed[i].variants[0].attributes.att_set_1.en.data;
for(var j=0;j<dataArray.length;j++){
var f = dataArray[j];
if((f.title=='Height'&&f.v>=2000){
foundHeight = true;
} else if(f.title=='Weight'&&f.v>=1000)){
foundWeight = true;
}
}
if(foundHeight && foundWeight)
returnArray.push(feed[i]);
}
}
getContext().getResponse().setBody(JSON.stringify(returnArray));
});
if (!isAccepted) throw new Error('The query was not accepted by the server.');
}
输出结果:
[
{
"id": "1",
"name": "My Product 1",
"variants": [
{
"id": 2179,
"code": "A",
"attributes": {
"att_set_1": {
"en": {
"name": "Attribute Set 1",
"data": [
{
"id": 919,
"title": "Height",
"label": "height_mm",
"v": 2330,
"unit": "mm"
},
{
"id": 921,
"title": "Weight",
"label": "weight",
"v": 2931,
"unit": "kg"
},
{
"id": 923,
"title": "Other",
"label": "blah",
"v": 200,
"unit": "mm"
}
]
}
}
}
}
]
},
{
"id": "3",
"name": "My Product 3",
"variants": [
{
"id": 2181,
"code": "C",
"attributes": {
"att_set_1": {
"en": {
"name": "Attribute Set 1",
"data": [
{
"id": 919,
"title": "Height",
"label": "height_mm",
"v": 2000,
"unit": "mm"
},
{
"id": 921,
"title": "Weight",
"label": "weight",
"v": 999,
"unit": "kg"
},
{
"id": 923,
"title": "Other",
"label": "blah",
"v": 200,
"unit": "mm"
}
]
}
}
}
}
]
}
]
这个结果应该是你想要的。您可以在门户网站或代码中创建它。如有任何疑虑,请告诉我。
希望它对你有所帮助。