With the source documents as:
INSERT INTO default VALUES("k0",{"a":1,"c":1}),VALUES("k1",{"b":2,"d":2});
You can:
INSERT INTO default
VALUES("newkey1",(SELECT default FROM default WHERE meta().id IN ["k0","k1"]));
which will result in a new document containing a unnamed array of the source documents, i.e.
SELECT * FROM default WHERE meta().id = 'newkey1';
"results": [
{
"default": [
{
"default": {
"a": 1,
"c": 1
}
},
{
"default": {
"b": 2,
"d": 2
}
}
]
}
],
Variants such as:
INSERT INTO default
VALUES("newkey2",{"docs":(SELECT default.* FROM default WHERE meta().id IN ["k0","k1"])});
return an object containing a named array and remove the inner labelling:
SELECT default.* FROM default WHERE meta().id = 'newkey2';
"results": [
{
"docs": [
{
"a": 1,
"c": 1
},
{
"b": 2,
"d": 2
}
]
}
],
If you’re looking to merge the matching documents into one, then:
INSERT INTO default
VALUES("newkey3",OBJECT v.name:v.val
FOR v IN object_pairs((SELECT default.* FROM default WHERE meta().id IN ["k0","k1"])[*]) END);
results in a document that contains all fields from all source documents, with each field’s value being an array of values, one from each document with null as the placeholder if a field didn’t appear in a document:
SELECT default.* FROM default WHERE meta().id = 'newkey3';
"results": [
{
"a": [
null,
1
],
"b": [
2,
null
],
"c": [
null,
1
],
"d": [
2,
null
]
}
],
The nulls can be stripped with:
INSERT INTO default
VALUES("newkey4",OBJECT v.name:ARRAY a FOR a IN v.val WHEN a IS NOT NULL END
FOR v IN object_pairs((SELECT default.* FROM default WHERE meta().id IN ["k0","k1"])[*]) END);
yielding:
SELECT default.* FROM default WHERE meta().id = 'newkey4';
"results": [
{
"a": [
1
],
"b": [
2
],
"c": [
1
],
"d": [
2
]
}
],
If you know you have only unique fields or care only for the first value for each:
INSERT INTO default VALUES("newkey5",OBJECT v.name:ARRAY a FOR a IN v.val WHEN a IS NOT NULL END[0] FOR v IN object_pairs((SELECT default.* FROM default WHERE meta().id IN ["k0","k1"])[*]) END);
or
INSERT INTO default VALUES("newkey5",OBJECT v.name:FIRST a FOR a IN v.val WHEN a IS NOT NULL END FOR v IN object_pairs((SELECT default.* FROM default WHERE meta().id IN ["k0","k1"])[*]) END);
yields:
SELECT default.* FROM default WHERE meta().id = 'newkey5';
"results": [
{
"a": 1,
"b": 2,
"c": 1,
"d": 2
}
],
(You could make this more complex with a CASE statement to unpack the array only when there is a single value etc.)
HTH.