Hi Geert,

Thanks.  I am not quite sure how /text() fits in this though :-)  Yes,
that's what I am doing now, converting the document to a plain object
in memory, before reverting it back to the document.  What I have now
instead of:

    xdmp.nodeInsertChild(
        fn.doc(uri).next().value.root.foo,
        { three: [ 'tres', 3, 'trois' ] });

is the following:

    var doc = fn.doc(uri).next().value.root;
    var obj = doc.toObject();
    obj.foo.three = [ 'tres', 3, 'trois' ];
    xdmp.documentInsert(uri, obj);

But this is rather a hack, as we do have a three node system available.

Regards,

-- 
Florent Georges
http://fgeorges.org/
http://h2oconsulting.be/


On 18 May 2016 at 09:54, Geert Josten wrote:
> Hi Florent,
>
> You are effectively passing an unnamed (root) object-node with a single
> property as argument, whereas the node function is expecting the (named)
> property itself. In XQuery you would append /text() after your JSON to fix
> that:
>
> xdmp:node-insert-child(doc("/test.json")/object-node(), object-node{ "c":
> "d" }/text())
>
>
> But I don¹t think that will work in JS..
>
> On the other hand, why don¹t you convert the JSON node to a real JS JSON
> object, manipulate that in the old fashion JS way, and insert that back
> into the db? Might be much more practical, particularly if you are making
> multiple changes..
>
> Cheers
>
> On 5/18/16, 9:13 AM, "[email protected] on behalf of
> Florent Georges" <[email protected] on behalf of
> [email protected]> wrote:
>
>>Hi,
>>
>>I am sure I am missing something simple here...  I am trying to add an
>>object node to a JSON doc stored in the database using JavaScript.
>>But all examples and documentation I can find are about either JavaScript
>>manipulating XML nodes, or XQuery manipulating JSON documents.
>>
>>Especially, in XQuery, one can use object-node {} to construct an object
>>node.  Which in turn can be used with e.g. xdmp:node-insert-child().
>>But if I try the following in JavaScript:
>>
>>    xdmp.nodeInsertChild(
>>        fn.doc(uri).next().value.root.foo,
>>        { three: [ 'tres', 3, 'trois' ] });
>>
>>then I get the following error:
>>
>>    [javascript] XDMP-CHILDUNNAMED: xdmp.nodeInsertChild(
>>        fn.head(xdmp.unpath("fn:doc('...')/foo")),
>>            {"three":["tres", 3, "trois"]})
>>        -- Object nodes cannot have unnamed children
>>
>>Any idea what (quite obvious I presume) I am missing?
>>
>>Regards,
>>
>>--
>>Florent Georges
>>http://fgeorges.org/
>>http://h2oconsulting.be/
>>_______________________________________________
>>General mailing list
>>[email protected]
>>Manage your subscription at:
>>http://developer.marklogic.com/mailman/listinfo/general
>
> _______________________________________________
> General mailing list
> [email protected]
> Manage your subscription at:
> http://developer.marklogic.com/mailman/listinfo/general
_______________________________________________
General mailing list
[email protected]
Manage your subscription at: 
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to