diff --git a/arangod/Ahuacatl/ahuacatl-functions.c b/arangod/Ahuacatl/ahuacatl-functions.c index a8abe314a7..bd57ba0fbc 100644 --- a/arangod/Ahuacatl/ahuacatl-functions.c +++ b/arangod/Ahuacatl/ahuacatl-functions.c @@ -480,7 +480,7 @@ TRI_associative_pointer_t* TRI_InitialiseFunctionsAql (void) { REGISTER_FUNCTION("HAS", "HAS", true, false, "az,s", NULL); REGISTER_FUNCTION("MERGE", "MERGE", true, false, "a,a|+", NULL); REGISTER_FUNCTION("MERGE_RECURSIVE", "MERGE_RECURSIVE", true, false, "a,a|+", NULL); - REGISTER_FUNCTION("DOCUMENT", "DOCUMENT", false, false, "h,.", NULL); + REGISTER_FUNCTION("DOCUMENT", "DOCUMENT", false, false, "h,sl", NULL); // geo functions REGISTER_FUNCTION("NEAR", "GEO_NEAR", false, false, "h,n,n,n|s", NULL); diff --git a/arangod/Documentation/aql.dox b/arangod/Documentation/aql.dox index 75cc588d08..36a95a36bc 100644 --- a/arangod/Documentation/aql.dox +++ b/arangod/Documentation/aql.dox @@ -981,7 +981,8 @@ /// in the specified collection. If there is a mismatch between the @FA{collection} passed and /// the collection specified in @FA{id}, then no document will be returned. Additionally, if the /// @FA{collection} matches the collection value specified in @FA{id} but the document cannot be -/// found, no document will be returned. +/// found, no document will be returned. This function also allows @FA{id} to be a list of ids. +/// In this case, the function will return a list of all documents that could be found. /// /// @section AqlOperations High-level operations /// diff --git a/js/server/ahuacatl.js b/js/server/ahuacatl.js index 75d26ce808..efc7d14555 100755 --- a/js/server/ahuacatl.js +++ b/js/server/ahuacatl.js @@ -412,10 +412,24 @@ function AHUACATL_LIST (value) { } //////////////////////////////////////////////////////////////////////////////// -/// @brief get a document by its unique id +/// @brief get a document by its unique id or their unique ids //////////////////////////////////////////////////////////////////////////////// function AHUACATL_DOCUMENT (collection, id) { + if (AHUACATL_TYPEWEIGHT(id) === AHUACATL_TYPEWEIGHT_LIST) { + var c = AHUACATL_COLLECTION(collection); + + var result = [ ]; + for (var i = 0; i < id.length; ++i) { + try { + result.push(c.document(id[i])); + } + catch (e) { + } + } + return result; + } + try { return AHUACATL_COLLECTION(collection).document(id); } diff --git a/js/server/tests/ahuacatl-functions.js b/js/server/tests/ahuacatl-functions.js index f81d85605c..cbcdf48fc5 100644 --- a/js/server/tests/ahuacatl-functions.js +++ b/js/server/tests/ahuacatl-functions.js @@ -53,6 +53,24 @@ function ahuacatlFunctionsTestSuite () { //////////////////////////////////////////////////////////////////////////////// function getQueryResults (query, isFlat, bindVars) { + var sanitize = function (row) { + var keys = [ ]; + for (var k in row) { + if (row.hasOwnProperty(k) && k != '_rev' && k != '_key' && k != '_id') { + keys.push(k); + } + } + + keys.sort(); + var resultRow = { }; + for (var k in keys) { + if (keys.hasOwnProperty(k)) { + resultRow[keys[k]] = row[keys[k]]; + } + } + return resultRow; + }; + var result = executeQuery(query, bindVars).getRows(); var results = [ ]; @@ -66,21 +84,16 @@ function ahuacatlFunctionsTestSuite () { results.push(row); } else { - var keys = [ ]; - for (var k in row) { - if (row.hasOwnProperty(k) && k != '_rev' && k != '_key' && k != '_id') { - keys.push(k); + if (Array.isArray(row)) { + var x = [ ]; + for (var j = 0; j < row.length; ++j) { + x.push(sanitize(row[j])); } + results.push(x); } - - keys.sort(); - var resultRow = { }; - for (var k in keys) { - if (keys.hasOwnProperty(k)) { - resultRow[keys[k]] = row[keys[k]]; - } + else { + results.push(sanitize(row)); } - results.push(resultRow); } } @@ -1128,6 +1141,43 @@ function ahuacatlFunctionsTestSuite () { internal.db._drop(cn); }, +//////////////////////////////////////////////////////////////////////////////// +/// @brief test document function +//////////////////////////////////////////////////////////////////////////////// + + testDocumentMulti1 : function () { + var cn = "UnitTestsAhuacatlFunctions"; + + internal.db._drop(cn); + var cx = internal.db._create(cn); + var d1 = cx.save({ "title" : "123", "value" : 456, "zxy" : 1 }); + var d2 = cx.save({ "title" : "nada", "value" : 123, "zzzz" : false }); + var d3 = cx.save({ "title" : "boom", "value" : 3321, "zzzz" : null }); + + var expected, actual; + + expected = [ [ + { title: "123", value : 456, zxy : 1 }, + { title: "nada", value : 123, zzzz : false }, + { title: "boom", value : 3321, zzzz : null } + ] ]; + + actual = getQueryResults("RETURN DOCUMENT(@@cn, @id)", false, { "@cn" : cn, "id" : [ d1._id, d2._id, d3._id ] }); + assertEqual(expected, actual); + + expected = [ [ { title: "nada", value : 123, zzzz : false } ] ]; + actual = getQueryResults("RETURN DOCUMENT(@@cn, @id)", false, { "@cn" : cn, "id" : [ d2._id ] }); + assertEqual(expected, actual); + + cx.remove(d3); + + expected = [ [ { title: "nada", value : 123, zzzz : false } ] ]; + actual = getQueryResults("RETURN DOCUMENT(@@cn, @id)", false, { "@cn" : cn, "id" : [ d2._id, d3._id, "abc/def" ] }); + assertEqual(expected, actual); + + internal.db._drop(cn); + }, + //////////////////////////////////////////////////////////////////////////////// /// @brief test document function ////////////////////////////////////////////////////////////////////////////////