I am trying to create N1QL Query to delete the JsonArraydocuments which are duplicated. Any help will be appreciated pls.
Sample:
Bucket name: “Apple”
Records format as follows
Document id = 1111
JsonArrrayContent is:
[{price:2111, type:“simla”, “color”:red, “time”: 01-Apr},
{price:2111, type:“simla”, “color”:red, “time”: 02-Apr} ]
I want to create a N1QL query so that, the above JSON array document shud become as follows
Once You have that. Assume ja is Jasson Array
Adjust GROUP BY on what fields you want latest (ex. you want one for color,type).
SELECT d.*, (SELECT RAW MAX([j.time,j])[1] FROM d.ja AS j GROUP BY j.type, j.color ) AS ja
FROM default AS d
WHERE .....;
UPDATE default AS d
SET d.ja = (SELECT RAW MAX([j.time,j])[1] FROM d.ja AS j GROUP BY j.type, j.color )
WHERE ...
Hi @vsr1 thanks for quick response.
It is not working. The SELECT query returns empty response,
Actually this is my input JSONArray Document stored in couchbase
[
{
“field0”: “0”,
“field1”: “aaaaaa”,
“field2”: “bbbbbb”,
“field3”: “cccccc”,
“field4”: 1111111,
“field5”: “dddddd”
},
{
“field0”: “0”,
“field1”: “aaaaaa”,
“field2”: “bbbbbb”,
“field3”: “cccccc”,
“field4”: 2222222,
“field5”: “dddddd”
}
]
the field4 is stored as long type (which you can see in the above JSON). All other fields are string type.
So now when i run a N1QL query it should return and update the JSON Array document which has maximum “field4” value.
in this case it should return the below and update the document
[
{
“field0”: “0”,
“field1”: “aaaaaa”,
“field2”: “bbbbbb”,
“field3”: “cccccc”,
“field4”: 2222222,
“field5”: “dddddd”
}
]
@vsr1 it works good now when i apply to single data or JSONArrayDocument
But somehow when i apply the N1QL you have provided, into my real couchbase data its not working.
My Couchbase records storage look exactly similar like this.
Example: Bucket-Name: test-bucket
My first record is
Key: 1111
Document Value is:
[
{
“field0”: “0”,
“field1”: “aaaaaa”,
“field2”: “bbbbbb”,
“field3”: “cccccc”,
“field4”: 1111111,
“field5”: “dddddd”
},
{
“field0”: “0”,
“field1”: “aaaaaa”,
“field2”: “bbbbbb”,
“field3”: “cccccc”,
“field4”: 2222222,
“field5”: “dddddd”
}
]
second record is
Key: 2222
Document Value is:
[
{
“field0”: “0”,
“field1”: “aaaaaa”,
“field2”: “bbbbbb”,
“field3”: “cccccc”,
“field4”: 3333,
“field5”: “dddddd”
},
{
“field0”: “0”,
“field1”: “aaaaaa”,
“field2”: “bbbbbb”,
“field3”: “cccccc”,
“field4”: 4444,
“field5”: “dddddd”
}
]
So when i execute the N1QL it should be executed in all the JSONDocuments/records that are stored in the couchbase and finally my couchbase records should like this:
First Record - After the N1QL query should look like
Key: 1111
Document Value is:
[
{
“field0”: “0”,
“field1”: “aaaaaa”,
“field2”: “bbbbbb”,
“field3”: “cccccc”,
“field4”: 2222222,
“field5”: “dddddd”
}
]
Second Record - After the N1QL query should look like
Key: 2222
Document Value is:
[
{
“field0”: “0”,
“field1”: “aaaaaa”,
“field2”: “bbbbbb”,
“field3”: “cccccc”,
“field4”: 4444,
“field5”: “dddddd”
}
]
and so on for all the records.
Any suggestions on where am i making mistake in the N1QL query?
i want to be make it like this =>
[
{
“field1”: “0000000”,
“field2”: aaaaa,
“field3”: bbbbbb,
“field4”: 111111,
“field5”: “qqqqq”,
},
{
“field1”: “00000000”,
“field2”: aaaa,
“field3”: bbbbb,
“field4”: 222222,
“field5”: “qqqqq”,
}
]
basically i dont want the outer-most square bracket in my result.
any way to get rid of that square bracket?