Good day Monks,

i'm trying to execute a simple insert into mongodb from dancer2, but i failwhen the insert statement call a stored function to determine the "_id" of the new document. my goal is to have as document is a simple unique auto incremental counter as documents ID.

First i save the function using mongo sheel,

> db.system.js.save({_id:"getNextSequence", value:function(name){ var +ret = db.counters.findAndModify({query: { _id: name },update: { $inc: + { seq: 1 }},new: true});return ret.seq; }});

then from Dancer2 post i try to insert the new documents:

post "/commNew" => require_login sub { my $userh = logged_in_user; my $username=$userh->{username}; my $adm=lc(config->{app}{commAdmin}); if($username =~ m/$adm/){ info ("User is $username for comm 2b" ); my $res=$comms->insert_one({"_id"=> getNextSequence("comm-id") +,"year" => config->{app}{year}}); my $lastid = $res->inserted_id; my @kuy = body_parameters->keys; info ("Insert int Mongo: ".$lastid); foreach my $oto ( @kuy ) { info ("key name value: " .$oto ." value: ".body_parameters +->get($oto)); $comms->update_one( {"_id" => $lastid},{'$set' => {$oto => + body_parameters->get($oto)}}); } info ("User is $username for comm 2z" ); }else{ return 404; } };

Instead of evaluate the stored function "getNextSequence", it try to add the document like { "_id" : "getNextSequence(\"comm-id\")",.....}

Where i'm wrong ?

thanks in advance


In reply to MongoDB Stored procedure from Dancer2 by actarus2003

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.