Object manipulation

n1ql

#1

dear all,
i’m wondering using n1ql how can i 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”
}
}

let us suppose that i want to prune out from tutorial all fields whose value is set to “Dave”

I’ve seen that N1ql provides function for array manipulation thus the following query allows me to generate an array of attributes-values that satisfy the condition.

SELECT
array f FOR f in OBJECT_pairs(tutorial) when f.value<>“Dave” END as f1
FROM tutorial

however, i would need to define from this array an object composed of all the listed attribute-values

is it possible to achieve this with n1ql?

thanks


#2

Hey @pietro.colombo,

Can you share your expected output so everyone is clear on what you’re looking for. It is appreciated that you supplied the original document.

Best,


#3

Yes, sure

I’m using the dataset of the interactive tutorial at http://query.pub.couchbase.com/tutorial

referring to previous example, i would like to prune out from the documents any attribute which is set to “Dave”,

in other words, let us newly consider the document

{
“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’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.

using the operator ARRAY - FOR - IN i can get all attributes of tutorial that satisfy a condition as elements of an array

thus, 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?

thanks


#4

did y get the reply from any other sorce,if yes could you please share with us


#5

Yes, I got an answer from geraldss
basically, the issue has been addressed with the introduction of the
operator object in Couchbase 4.5, which allows one to build a new object
as composed of fields of another object that satisfy given conditions.

best regards

Pietro