I'm using couchDB in the fabric for state database in peers. I have a json data that is stored inside the ledger
JSON DATA
"Company": {
"Associate": {
"entity": {
"name": "foo",
"role": "admin"
},
"access": [
{
"create": "allowed"
}
]
}
}
}
I have to query the data based on access json object, like these have values like "create","readonly","delete" etc..
Currently I tried this but none of the records came up.
{
"selector": {
"Company": {
"Associate": {
"access": {
"$elemMatch": {
"$eq": "create"
}
}
}
}
}
}
How can I query the data's ?
I think you want (I use dot notation for simplicity):
{
"selector": {
"Company.Associate.access": {
"$elemMatch": {
"create": {
"$exists": true
}
}
}
}
}
...or maybe...
{
"selector": {
"Company.Associate.access": {
"$elemMatch": {
"create": "allowed"
}
}
}
}
I need to get the documents where exists Archery in the array list of games. How can i do with CouchDB selector?
[{
"name": "John",
"games": ["Archery", "Board sports"]
},
{
"name": "Sara",
"games": ["Fishing", "Archery"]
},
{
"name": "Tara",
"games": ["Decathlon"]
}]
You can use $elemMatch:
{
"selector": {
"games": {
"$elemMatch": {
"$eq": "Archery"
}
}
}
}
It will return all objects where games field equals 'Archery'
Imagine I have a movie document, and its ratings is modelled as nested fields:
"mappings": {
"movie": {
"properties": {
"name": {"type": "text"}
"ratings": {
"type": "nested"
"properties": {
"userId": {"type": "keyword"},
"rating": {"type": "integer"}
}
}
}
}
}
What I want to do is: for a given movie name, and a list of users' ids. I want to find the movie and lowest rating among these users. I managed to construct a query to do the job
{
"query": {
"bool": {
"must": [{
"match": {
"name": "fake movie name"
}
}],
"filter": {
"nested": {
"path": "ratings",
"query": {
"bool": {
"must": {
"match": {
"ratings.userId": ["user1", "user2"]
}
}
}
}
}
}
},
"aggs": {
"userIdFilter": {
"filter": {
"terms": {
"ratings.userId": ["user1", "user2"]
}
},
"aggs": {
"lowestRating": {
"min": {
"field": "ratings.rating"
}
}
}
}
}
}
}
Is possible to add filter on the lowest rating, only returns document's lowest rating is lower certain value?
I hope there is a way to approach this without using script, I tried bucket-selector-aggregation, but cannot get a working version. Any ideas?
Thank you
How can I get only one item when querying an array on CloudantDB?
Example document :
"category": {
"sub_category": [
{
"category_id": "127"
},
{
"category_id": "128"
}
],
}
query :
{
"selector": {
"sub_category": {
"$elemMatch": {
"category_id": "127"
}
}
}
}
wish result document:
"category": {
"sub_category": [
{
"category_id": "127"
}
],
}
You can't do that with Mango queries. You can accomplish something similar with a combination of a traditional view, and a show function.
I am using elasticsearch in a node environment.
This is my data model :
"_source": {
"repo_id": "ecom-test",
"pr_id": "1",
"pr_status": "approved",
"date_created": "2016-12-14T12:55:50.317Z",
"date_modified": "2016-12-14T12:55:50.317Z",
"comments": [
{
"tuple": {
"reviewee": "Max",
"reviewer": "Vic"
},
"nb": "1",
"type": "typo"
},
{
"tuple": {
"reviewee": "Antoine",
"reviewer": "Vic"
},
"nb": "2",
"type": "logic"
}
]
}
I add comments using the following code :
client.update({
index: 'loreal',
type: 'reviews',
id: reviewID,
body: {
script: {
inline: "ctx._source.comments.add(params.comment)",
lang: "painless",
params: {
comment: {
tuple: {
reviewee: data.reviewee,
reviewer: data.reviewer
},
nb: data.nb,
type: data.type
}
}
}
}
})
But when a comment with the same "reviewee", "reviewer" AND "logic" already exists i would like to update the "nb" attribute instead of creating a new item in the "comments" array.
I thought i would first make a search but I can't find a way to make a search that matches any element of the array "comments" that has those three values.
I really hope you guys can give me a hand with this, it's been a long time I am searching =s.
Thanks in advance
Given the struct of your document, I would have defined the comment field like a nested object: see the documentation example which is pretty close to your use case: https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-objects.html#nested-objects
Considering that you got in param the reviewee value, I would use the combination of nested query and bool query. https://www.elastic.co/guide/en/elasticsearch/guide/2.x/nested-query.html
{
"query": {
"nested": {
"path": "comments",
"query": {
"bool": {
"must": [{
"match": {
"comments.tuple.reviewee": "your reviewee"
}
}, {
"match": {
"comments.tuple.reviewer": "your reviewee"
}
}, {
"term": {
"comments.type": "logic"
}
}]
}
}
}
}
Here the test I wrote:
PUT test
{
"mappings": {
"item": {
"properties": {
"comments": {
"type": "nested"
}
}
}
}
}
POST test/item
{ "comments": [
{
"tuple": {
"reviewee": "Max",
"reviewer": "Max"
},
"nb": "1",
"type": "logic"
},
{
"tuple": {
"reviewee": "Antoine",
"reviewer": "Vic"
},
"nb": "2",
"type": "logic"
}
]
}
POST test/item
{
"comments": [
{
"tuple": {
"reviewee": "Max",
"reviewer": "Max"
},
"nb": "1",
"type": "test"
},
{
"tuple": {
"reviewee": "Antoine",
"reviewer": "Vic"
},
"nb": "2",
"type": "logic"
}
]
}
POST test/item
{
"comments": [
{
"tuple": {
"reviewee": "Max",
"reviewer": "Mac"
},
"nb": "1",
"type": "logic"
},
{
"tuple": {
"reviewee": "Antoine",
"reviewer": "Vic"
},
"nb": "2",
"type": "logic"
}
]
}
GET /test/item/_search
{
"query": {
"nested": {
"path": "comments",
"query": {
"bool": {
"must": [{
"match": {
"comments.tuple.reviewee": "Max"
}
}, {
"match": {
"comments.tuple.reviewer": "Max"
}
}, {
"term": {
"comments.type": "logic"
}
}]
}
}
}
}}