dear all
I’m using the dataset of the interactive tutorial at http://query.pub.couchbase.com/tutorial
I would like to find a way with N1QL to remove from an object all key value attributes that do not satisfy a given condition.
for instance, let us consider this doc
{
“tutorial”: {
“age”: 46,
“children”: [
{
“age”: 17,
“fname”: “Aiden”,
“gender”: “m”
},
{
“age”: 2,
“fname”: “Bill”,
“gender”: “f”
}
],
“email”: "dave@gmail.com",
“fname”: “Dave”,
“hobbies”: [
“golf”,
“surfing”
],
“lname”: “Smith”,
“relation”: “friend”,
“title”: “Mr.”,
“type”: “contact”
}
}
i would like to prune out from tutorial all fields whose value is set to “Dave”
i’m expecting to get :
{
“tutorial”: {
“age”: 46,
“children”: [
{
“age”: 17,
“fname”: “Aiden”,
“gender”: “m”
},
{
“age”: 2,
“fname”: “Bill”,
“gender”: “f”
}
],
“email”: "dave@gmail.com",
“hobbies”: [
“golf”,
“surfing”
],
“lname”: “Smith”,
“relation”: “friend”,
“title”: “Mr.”,
“type”: “contact”
}
}
which is an object from which the pair “fname”: “Dave” has been removed.
I have found that using the operator ARRAY - FOR - IN allows me to derive all attributes of document tutorial that satisfy the condition
in the form of name-value pairs.
Executing the query
SELECT
array f FOR f in OBJECT_pairs(tutorial) when f.value<>“Dave” END as f1
FROM tutorial
I get the document:
{
“f1”: [
{
“name”: “age”,
“value”: 46
},
{
“name”: “children”,
“value”: [
{
“age”: 17,
“fname”: “Aiden”,
“gender”: “m”
},
{
“age”: 2,
“fname”: “Bill”,
“gender”: “f”
}
]
},
{
“name”: “email”,
“value”: "dave@gmail.com"
},
{
“name”: “hobbies”,
“value”: [
“golf”,
“surfing”
]
},
{
“name”: “lname”,
“value”: “Smith”
},
{
“name”: “relation”,
“value”: “friend”
},
{
“name”: “title”,
“value”: “Mr.”
},
{
“name”: “type”,
“value”: “contact”
}
]
},
but my goal is returning an object like this:
{
“tutorial”: {
“age”: 46,
“children”: [
{
“age”: 17,
“fname”: “Aiden”,
“gender”: “m”
},
{
“age”: 2,
“fname”: “Bill”,
“gender”: “f”
}
],
“email”: "dave@gmail.com",
“hobbies”: [
“golf”,
“surfing”
],
“lname”: “Smith”,
“relation”: “friend”,
“title”: “Mr.”,
“type”: “contact”
}
}
is this possible?
I have seen that https://github.com/couchbaselabs/query/blob/master/docs/n1ql-select.md
describes an operator : object name-expression : “expression for var in collection …” and i guess that such an operator could introduce the functionality that I need. I have tried it but it seems it is not supported. Is there any plan to support this or similar object manipulation operators in future versions of Couchbase / N1QL ? or do you know any way to handle this type of query with N1QL with current operators?
thank you,
Pietro