Reputation: 269
I've been using MongoDB and like it but have recently started working with Couchbase. I'm having some trouble retrieving documents in the way I'd like.
Let's say I have a document like this:
{
"data": [
{
"target": "value1"
},
{
"target": "value2"
},
{
"target": "value3"
}
]
}
In MongoDB it's possible to do something along the lines of find({"data.target": value2})
(as opposed to having to specify data.1.target
). However I can't seem to find a way to do this in Couchbase.
Is there a way, either with N1QL or a view, to find all documents based on a specified child key without specifying all the intermediate keys? I can get the document without any issue if I specify that it's data.1.target
, but that doesn't do me any good if I don't already know the key of the desired child array.
Put another way, For N1QL I'd like to be able to do the equivalent of using data[*].target
but after the WHERE
clause.
Sorry if I'm not clear. I've looked around quite a bit but I don't exactly know how to articulate my question or what particular keywords I should be searching for. I'm out of ideas of what to search for so I'm finally just asking here.
Thanks in advance for your time.
Upvotes: 0
Views: 593
Reputation: 56
You can use the any with within clause to find documents based on a specified child key. In your case the n1ql query will be: select * from default where any t within data satisfies t.target="value2" end;
Above query gives you all docs where data[*].target is equal to "value2" Within is very powerful as it will search nested arrays within object also.
In your case you can also use any with in clause, similar to what we have in our tutorial link: http://query.pub.couchbase.com/tutorial/#12 Your specific query will be: select * from default where any t in data satisfies t.target="value2" end; But this query will only search in 1 level inside array.
Upvotes: 3