admin管理员组文章数量:1406060
My DB instance contains some documents with the following schema:
{
id: <someGuid>
myJsonArray: [
{
subId: SmallUniqueId1,
val: <someValue>
},
{
subId: SmallUniqueId2,
val: <someOtherValue>
}
]
}
I have a Web API that generates new SubIds (you can think of them as randomly generated base64 strings that have a max length of 5 chars).
I'd like to query my DB to verify that the newly minted SubId doesn't already exist somewhere in all of my docs.
I figured the solution probably involves plugging in the new subId value into a WHERE clause (I'm hitting CosmosDB via the REST API). Here's what I don't know:
- I don't know how to query over every element of each document's JsonArray (if it was just querying on a single string field, it would be easy)
- I'm also not sure about the efficiency of this query and whether this approach for validating new Ids against what's already in the DB is, in general, a bad pattern. I have a one partition CosmosDB instance - the subId validation check should not occur very often.
My DB instance contains some documents with the following schema:
{
id: <someGuid>
myJsonArray: [
{
subId: SmallUniqueId1,
val: <someValue>
},
{
subId: SmallUniqueId2,
val: <someOtherValue>
}
]
}
I have a Web API that generates new SubIds (you can think of them as randomly generated base64 strings that have a max length of 5 chars).
I'd like to query my DB to verify that the newly minted SubId doesn't already exist somewhere in all of my docs.
I figured the solution probably involves plugging in the new subId value into a WHERE clause (I'm hitting CosmosDB via the REST API). Here's what I don't know:
- I don't know how to query over every element of each document's JsonArray (if it was just querying on a single string field, it would be easy)
- I'm also not sure about the efficiency of this query and whether this approach for validating new Ids against what's already in the DB is, in general, a bad pattern. I have a one partition CosmosDB instance - the subId validation check should not occur very often.
- Hi, any updates now ? – Jay Gong Commented Oct 12, 2017 at 2:58
- Give me a few more days to get back to you - had a few fires to put out. I'm intrigued by the UDF suggestion at the bottom! (Thank you!) I have a scenario where I need to migrate these docs from one cosmosdb instance to another, and there's no guarantee that these subIds (which are valid in db1) are ALSO valid in db2. I need to think that through a bit before following up. – nciao Commented Oct 12, 2017 at 14:29
- Sure! Waiting for your reply. – Jay Gong Commented Oct 13, 2017 at 1:24
1 Answer
Reset to default 5I don't know how to query over every element of each document's JsonArray (if it was just querying on a single string field, it would be easy)
I created two sample documents in my azure cosmosdb
as you described.
[
{
"id": "id1",
"myJsonArray": [
{
"subId": "sub1",
"val": "value1"
},
{
"subId": "sub2",
"val": "value2"
}
],
},
{
"id": "id2",
"myJsonArray": [
{
"subId": "sub3",
"val": "value3"
},
{
"subId": "sub4",
"val": "value4"
}
],
}
]
You could use the SQL
below to query field
in array.
SELECT a.subId as subId FROM c
join a in c.myJsonArray
Result:
Add where clause
.
SELECT a.subId as subId FROM c
join a in c.myJsonArray
where a.subId='sub1'
Result:
I'm also not sure about the efficiency of this query and whether this approach for validating new Ids against what's already in the DB is, in general, a bad pattern. I have a one partition CosmosDB instance - the subId validation check should not occur very often.
As you needs,I thought you could use User Defined Function
in Azure Cosmos DB.
Invoke UDF
when you create document into your DB, check if the field value already exists, return the boolean
result.
Please refer to this official doc.
Hope it helps you. Any concern,please feel free to let me know.
本文标签: javascriptCosmosDB how to query for the existence of a value nested in a json arrayStack Overflow
版权声明:本文标题:javascript - CosmosDB: how to query for the existence of a value nested in a json array? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744958691a2634516.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论