Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

MongoDB ObjectID

by betacentauri (Sexton)
on Oct 12, 2019 at 17:36 UTC ( [id://11107380]=perlquestion: print w/replies, xml ) Need Help??

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

O monks,

I can't for the life of me figure out the proper way to prepare HTML references to MongoDB documents. I'm using Dancer2 with MongoDB 3.4.15, and Template Toolkit. I'm fairly new to these three monsters, and it shows.

I can select and display documents from a collection in an HTML table. When a table row is clicked, I want to show a modal window filled with the document data, then possibly edit some data, then possibly save it back.

My problem lies in how to build the table. I envision every row provisioned with an onclick script which receives the document _id and uses it as a key to bring the document from the database via AJAX for the modal to be filled. Now, how to go about presenting _id, which is an ObjectID, to Template Toolkit as valid HTML content for the script to consume it? As per Data::Dumper, each _id field data is '_id' => bless( {'oid' => '...funny characters...' }, 'BSON::OID' ). Understandably, I can't seem to make any other than HASHrefs to reach my HTML.

Should I serialize every ObjectID on the way to HTML, then deserialize it to get an ObjectID again? How to achieve this? If I were to extract oid, then convert it to some visible form, could this be done into the template? I've tried with a $project stage in the aggregation pipeline but I don't understand how or where to convert oids to strings.

Am I misdirected? Is there a simple way to do this? Thank you in advance.

Replies are listed 'Best First'.
Re: MongoDB ObjectID
by 1nickt (Canon) on Oct 12, 2019 at 20:55 UTC

    Hi, good choice of tools!

    "Should I serialize every ObjectID on the way to HTML"

    Yes. You should pass the data for your table to your template as a hashref or arrayref of arrayrefs, serialized to JSON. (And make calls to your Dancer app passing JSON representing SQL ops.) It'll be a long string (or you use paging).

    Then use a front-end framework for building and displaying the actual HTML table. I have had great success using the DataTable jQuery plugin with Dancer2 and TT2. It has great support for autoscrolling and callbacks for making cell data click into other things. There's an add-on that provides interactive CRUD ops to your server via Ajax, and also of possible note is DataTables (and maybe Web::Library::DataTables).

    Hope this helps! Glad to help with Dancer2 questions here!


    The way forward always starts with a minimal test.
      Wow, thank you very much! What a juicy answer! Please, please, elaborate about "or you use paging".

        Depending on the framework you use in the browser to display the table, you may have too many rows in your table to transmit all at once. If so, you send a subset of the rows to be displayed, along with the data that the front-end JS needs to fetch the next "page" of rows (basically just a starting row ID and an offset).

        FWIW you can comfortably avoid paging and preload up to about 10,000 rows in a JSON object that you pass to the browser via your template layer, and DataTables will handle it smoothly (it's pretty nifty and only adds the row into the DOM when the user scrolls it into view).

        Hope this helps!


        The way forward always starts with a minimal test.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11107380]
Approved by marto
Front-paged by 1nickt
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-03-28 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found