actarus2003 has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: MongoDB Stored procedure from Dancer2
by Corion (Patriarch) on Mar 28, 2017 at 08:25 UTC

    According to MongoDB::Tutorial, you are supposed to not pass in _id at all to have MongoDB issue the id. If you want to hand out ids from a specific sequence, I don't know how to do that though.

    I don't find any mention of "stored procedure" in the MongoDB distribution. I don't know how the API, and the closest I found was some random blog post on how to call server-side Javascript from Python. Most likely you have to send the same/similar data structure to make MongoDB do your bidding from Perl.

    Update: See below for the official documentation on how to create an autoincrement field with MongoDB.

      Thanks for replay, yes i try to use the code mentioned in the example, because the javascript procedure are volatile (as disconnect they disappear and you need to insert again) i save them. problem is when you try to add a document as example say ({"_id"=> getNextSequence("comm-id") +,"year" => "2017"}) it work fine, getNexSequence javascript work fine. but if i call it from perl it is not recognized and executed, so id will be 'getNextSequence("comm-id")' string

        All I can suggest is the perl equivalent of the getNextSequence function

        my $filter = {'_id' => 'userid'}; my $update = { '$inc' => { 'seq' => 1} }; my $col = $db->get_collection('counters'); $col->update($filter,$update); my $id = $col->find_one($filter)->{'seq'}; print $id;

        You can probably use find_one_and_update() but it didn't work on my old version 0.45 of MongoDB

        poj

        The example seems to send raw Javascript instead of JSON or BSON.

        I don't find any example in the MongoDB documentation that shows how to send raw Javascript.

Re: MongoDB Stored procedure from Dancer2
by poj (Abbot) on Mar 28, 2017 at 08:42 UTC

    Are you using the example described here

    poj