Question on MongoDB and Dancer
Hi Dancers, I am pretty new to Dancer and really like the light nature of the framework. I am trying to create a route which will show data from a MongoDB collection and struggling with the syntax. I have the following in my app: sub connect_mongo { my $client = MongoDB->connect('mongodb://a.b.c.d') or die "Error connecting to Mongo"; my $db = $client->get_database( 'lfi-perl' ); return($db); } get '/mongo' => sub { my $db = connect_mongo(); my $docs = $db->get_collection( 'Test-Collection' ); my $all_docs; template 'show_mongo.tt', { 'entries' => $all_docs = $docs->find(), }; }; Then in my view the following: [% FOREACH id IN entries.keys.nsort %] [% entries.$id._id %] I am not able to see any data in my view but am also not seeing any errors. Any examples of getting data from Mongo in Dancer would be really helpful. Thank you in advance, Gavin Best Regards, Gavin Colborne Managing Director, Tele: 0207 193 2014 Mobile: 0788 400 4339 Skype: gavincolborne Visit: www.littleforest.co.uk [image: Little Forest LFi] <http://www.littleforest.co.uk/>
Have you enabled Template Toolkit in the config? -- Clive Eisen GPG: 75056DD0
On 18 Oct 2017, at 06:28, Gavin Colborne <gavin@littleforest.co.uk> wrote:
Hi Dancers,
I am pretty new to Dancer and really like the light nature of the framework.
I am trying to create a route which will show data from a MongoDB collection and struggling with the syntax.
I have the following in my app:
sub connect_mongo { my $client = MongoDB->connect('mongodb://a.b.c.d') or die "Error connecting to Mongo"; my $db = $client->get_database( 'lfi-perl' ); return($db); }
get '/mongo' => sub { my $db = connect_mongo(); my $docs = $db->get_collection( 'Test-Collection' ); my $all_docs; template 'show_mongo.tt <http://show_mongo.tt/>', { 'entries' => $all_docs = $docs->find(), }; };
Then in my view the following:
[% FOREACH id IN entries.keys.nsort %]
[% entries.$id._id %]
I am not able to see any data in my view but am also not seeing any errors.
Any examples of getting data from Mongo in Dancer would be really helpful.
Thank you in advance,
Gavin
Best Regards,
Gavin Colborne
Managing Director,
Tele: 0207 193 2014 Mobile: 0788 400 4339 Skype: gavincolborne Visit: www.littleforest.co.uk <http://www.littleforest.co.uk/>
<http://www.littleforest.co.uk/> _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
Hi Clive, Thanks for your response, yes I have enabled the Template Toolkit and have it working nicely with mysql data. I am struggling with a data format mis-match from the Mongo response I suspect... <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail> Virus-free. www.avg.com <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail> <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> Best Regards, Gavin Colborne Managing Director, Tele: 0207 193 2014 Mobile: 0788 400 4339 Skype: gavincolborne Visit: www.littleforest.co.uk [image: Little Forest LFi] <http://www.littleforest.co.uk/> On 18 October 2017 at 06:31, Clive Eisen <clive@hildebrand.co.uk> wrote:
Have you enabled Template Toolkit in the config?
-- Clive Eisen GPG: 75056DD0
On 18 Oct 2017, at 06:28, Gavin Colborne <gavin@littleforest.co.uk> wrote:
Hi Dancers,
I am pretty new to Dancer and really like the light nature of the framework.
I am trying to create a route which will show data from a MongoDB collection and struggling with the syntax.
I have the following in my app:
sub connect_mongo { my $client = MongoDB->connect('mongodb://a.b.c.d') or die "Error connecting to Mongo"; my $db = $client->get_database( 'lfi-perl' ); return($db); }
get '/mongo' => sub { my $db = connect_mongo(); my $docs = $db->get_collection( 'Test-Collection' ); my $all_docs; template 'show_mongo.tt', { 'entries' => $all_docs = $docs->find(), }; };
Then in my view the following:
[% FOREACH id IN entries.keys.nsort %]
[% entries.$id._id %]
I am not able to see any data in my view but am also not seeing any errors.
Any examples of getting data from Mongo in Dancer would be really helpful.
Thank you in advance,
Gavin
Best Regards,
Gavin Colborne
Managing Director,
Tele: 0207 193 2014 Mobile: 0788 400 4339 Skype: gavincolborne Visit: www.littleforest.co.uk
[image: Little Forest LFi] <http://www.littleforest.co.uk/> _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
I've been caught by that before. Try [% entries.$id.item('_id') %]. For some reason (would interested to learn what), TT doesn't seem to be able to cope with leading underscores in data structures. You can prove entries contains data using: [% USE Dumper(Indent=1) %] [% FOREACH id IN entries %] <pre>[% Dumper.dump(id) %]</pre> [% END %] or just [% Dumper.dump(entries) %] On 18/10/2017 06:28, Gavin Colborne wrote:
Hi Dancers,
I am pretty new to Dancer and really like the light nature of the framework.
I am trying to create a route which will show data from a MongoDB collection and struggling with the syntax.
I have the following in my app:
sub connect_mongo { my $client = MongoDB->connect('mongodb://a.b.c.d') or die "Error connecting to Mongo"; my $db = $client->get_database( 'lfi-perl' ); return($db); }
get '/mongo' => sub { my $db = connect_mongo(); my $docs = $db->get_collection( 'Test-Collection' ); my $all_docs; template 'show_mongo.tt <http://show_mongo.tt>', { 'entries' => $all_docs = $docs->find(), }; };
Then in my view the following:
[% FOREACH id IN entries.keys.nsort %] [% entries.$id._id %]
I am not able to see any data in my view but am also not seeing any errors.
Any examples of getting data from Mongo in Dancer would be really helpful.
Thank you in advance,
Gavin
Best Regards,
Gavin Colborne
Managing Director,
Tele: 0207 193 2014 Mobile: 0788 400 4339 Skype: gavincolborne Visit: www.littleforest.co.uk <http://www.littleforest.co.uk/>
Little Forest LFi <http://www.littleforest.co.uk/>
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-- Richard Jones
Thanks Richard, that looks really helpful, I will try it later on today and let you know :) <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail> Virus-free. www.avg.com <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail> <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> Best Regards, Gavin Colborne Managing Director, Tele: 0207 193 2014 Mobile: 0788 400 4339 Skype: gavincolborne Visit: www.littleforest.co.uk [image: Little Forest LFi] <http://www.littleforest.co.uk/> On 18 October 2017 at 10:13, Richard Jones <ra.jones@dpw.clara.co.uk> wrote:
I've been caught by that before. Try [% entries.$id.item('_id') %]. For some reason (would interested to learn what), TT doesn't seem to be able to cope with leading underscores in data structures. You can prove entries contains data using: [% USE Dumper(Indent=1) %] [% FOREACH id IN entries %] <pre>[% Dumper.dump(id) %]</pre> [% END %]
or just [% Dumper.dump(entries) %]
On 18/10/2017 06:28, Gavin Colborne wrote:
Hi Dancers,
I am pretty new to Dancer and really like the light nature of the framework.
I am trying to create a route which will show data from a MongoDB collection and struggling with the syntax.
I have the following in my app:
sub connect_mongo { my $client = MongoDB->connect('mongodb://a.b.c.d') or die "Error connecting to Mongo"; my $db = $client->get_database( 'lfi-perl' ); return($db); }
get '/mongo' => sub { my $db = connect_mongo(); my $docs = $db->get_collection( 'Test-Collection' ); my $all_docs; template 'show_mongo.tt', { 'entries' => $all_docs = $docs->find(), }; };
Then in my view the following:
[% FOREACH id IN entries.keys.nsort %]
[% entries.$id._id %]
I am not able to see any data in my view but am also not seeing any errors.
Any examples of getting data from Mongo in Dancer would be really helpful.
Thank you in advance,
Gavin
Best Regards,
Gavin Colborne
Managing Director,
Tele: 0207 193 2014 Mobile: 0788 400 4339 Skype: gavincolborne Visit: www.littleforest.co.uk
[image: Little Forest LFi] <http://www.littleforest.co.uk/>
_______________________________________________ dancer-users mailing listdancer-users@dancer.pmhttp://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-- Richard Jones
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
On 18/10/17 8:13 pm, Richard Jones wrote:
I've been caught by that before. Try [% entries.$id.item('_id') %]. For some reason (would interested to learn what), TT doesn't seem to be able to cope with leading underscores in data structures.
That's documented in Template::Manual::Variables, which states
Any key in a hash which starts with a '_' or '.' character will be considered private ...
Hope that helps, Russell. aka veryrusty.
On 18/10/2017 10:42, Russell Jenkins wrote:
On 18/10/17 8:13 pm, Richard Jones wrote:
I've been caught by that before. Try [% entries.$id.item('_id') %]. For some reason (would interested to learn what), TT doesn't seem to be able to cope with leading underscores in data structures.
That's documented in Template::Manual::Variables, which states
Any key in a hash which starts with a '_' or '.' character will be considered private ...
Thanks Russell, that's exactly it. Obviously I didn't RTFM (or had forgotten I'd done it eons ago). -- Richard Jones
Here is what I had to do, i was pulling from elasticsearch same issue as you, after a few hours i humbled myself and went on to irc perl help channel and someone mentioned the private variables not displaying, dangit how dumb. Here is my yaml config file for dancer2, the show_private_variables will allow _var's to work. template: "template_toolkit" engines: template: template_toolkit: start_tag: '[%' end_tag: '%]' show_private_variables: true; On Wed, Oct 18, 2017 at 4:04 AM, Richard Jones <ra.jones@dpw.clara.co.uk> wrote:
On 18/10/2017 10:42, Russell Jenkins wrote:
On 18/10/17 8:13 pm, Richard Jones wrote:
I've been caught by that before. Try [% entries.$id.item('_id') %]. For some reason (would interested to learn what), TT doesn't seem to be able to cope with leading underscores in data structures.
That's documented in Template::Manual::Variables, which states
Any key in a hash which starts with a '_' or '.' character will be considered private ...
Thanks Russell, that's exactly it. Obviously I didn't RTFM (or had forgotten I'd done it eons ago).
-- Richard Jones
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
Thanks Richard and everyone, I added the ".item" so my view shows: [% entries.$id.item('time') %] I still don't see any results :( I now see in my terminal the following: Argument "client" isn't numeric in numeric comparison (<=>) Which from what I can tell from Gabor's great site https://perlmaven.com/argument-isnt-numeric-in-numeric is some numeric operation problem. Do you have any examples of controllers passing Mongo data to views so I can compare? My code so far - perhaps the controller is the issue? sub connect_mongo { my $client = MongoDB->connect('mongodb://a.b.c.d') or die "Error connecting to Mongo"; my $db = $client->get_database( 'lfi-perl' ); return($db); } get '/mongo' => sub { my $db = connect_mongo(); my $docs = $db->get_collection( 'Test-Collection' ); my $all_docs; template 'show_mongo.tt', { 'entries' => $all_docs = $docs->find(), }; }; Appreciate your help, Gavin Best Regards, Gavin Colborne Managing Director, Tele: 0207 193 2014 Mobile: 0788 400 4339 Skype: gavincolborne Visit: www.littleforest.co.uk [image: Little Forest LFi] <http://www.littleforest.co.uk/> On 18 October 2017 at 10:13, Richard Jones <ra.jones@dpw.clara.co.uk> wrote:
I've been caught by that before. Try [% entries.$id.item('_id') %]. For some reason (would interested to learn what), TT doesn't seem to be able to cope with leading underscores in data structures. You can prove entries contains data using: [% USE Dumper(Indent=1) %] [% FOREACH id IN entries %] <pre>[% Dumper.dump(id) %]</pre> [% END %]
or just [% Dumper.dump(entries) %]
On 18/10/2017 06:28, Gavin Colborne wrote:
Hi Dancers,
I am pretty new to Dancer and really like the light nature of the framework.
I am trying to create a route which will show data from a MongoDB collection and struggling with the syntax.
I have the following in my app:
sub connect_mongo { my $client = MongoDB->connect('mongodb://a.b.c.d') or die "Error connecting to Mongo"; my $db = $client->get_database( 'lfi-perl' ); return($db); }
get '/mongo' => sub { my $db = connect_mongo(); my $docs = $db->get_collection( 'Test-Collection' ); my $all_docs; template 'show_mongo.tt', { 'entries' => $all_docs = $docs->find(), }; };
Then in my view the following:
[% FOREACH id IN entries.keys.nsort %]
[% entries.$id._id %]
I am not able to see any data in my view but am also not seeing any errors.
Any examples of getting data from Mongo in Dancer would be really helpful.
Thank you in advance,
Gavin
Best Regards,
Gavin Colborne
Managing Director,
Tele: 0207 193 2014 Mobile: 0788 400 4339 Skype: gavincolborne Visit: www.littleforest.co.uk
[image: Little Forest LFi] <http://www.littleforest.co.uk/>
_______________________________________________ dancer-users mailing listdancer-users@dancer.pmhttp://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-- Richard Jones
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
On Thu, 19 Oct 2017 07:22:46 +0100 Gavin Colborne <gavin@littleforest.co.uk> wrote:
Thanks Richard and everyone,
I added the ".item" so my view shows: [% entries.$id.item('time') %] I still don't see any results :(
I now see in my terminal the following: Argument "client" isn't numeric in numeric comparison (<=>) Which from what I can tell from Gabor's great site https://perlmaven.com/argument-isnt-numeric-in-numeric is some numeric operation problem.
That's because you're using "entries.keys.nsort". If you change that to "entries.keys.sort" you won't get the warning. If "client" is a key, you don't want a numeric sort.
Do you have any examples of controllers passing Mongo data to views so I can compare?
The "$docs->find()" call returns a cursor object. You can read about that in the MongoDB::Cursor man page. The last thing you want to do is iterate over its keys. If you use the "all" method on the cursor, it'll return an array of documents. So your "entries" key in the template vars hash could be done like this: template 'show_mongo.tt', { 'entries' => $all_docs = [ $docs->find()->all ], }; And then in the view: [% FOREACH doc IN entries %] [% doc.time %] [% END %] Another way to do it would be to use the cursor directly in the template (in case you have a humongous dataset). So your controller would pass the cursor to the template like this: template 'show_mongo.tt', { 'cursor' => $docs->find(), }; and in the view: [% WHILE cursor.has_next; SET doc = cursor.next %] [% doc.time %] [% END %] Then you don't need to store the entire dataset in memory. You'll only look at one document at a time.
My code so far - perhaps the controller is the issue?
sub connect_mongo { my $client = MongoDB->connect('mongodb://a.b.c.d') or die "Error connecting to Mongo"; my $db = $client->get_database( 'lfi-perl' ); return($db); }
get '/mongo' => sub { my $db = connect_mongo(); my $docs = $db->get_collection( 'Test-Collection' ); my $all_docs; template 'show_mongo.tt', { 'entries' => $all_docs = $docs->find(), }; };
Appreciate your help,
Gavin
Best Regards,
Gavin Colborne
Managing Director,
Tele: 0207 193 2014 Mobile: 0788 400 4339 Skype: gavincolborne Visit: www.littleforest.co.uk
[image: Little Forest LFi] <http://www.littleforest.co.uk/>
On 18 October 2017 at 10:13, Richard Jones <ra.jones@dpw.clara.co.uk> wrote:
I've been caught by that before. Try [% entries.$id.item('_id') %]. For some reason (would interested to learn what), TT doesn't seem to be able to cope with leading underscores in data structures. You can prove entries contains data using: [% USE Dumper(Indent=1) %] [% FOREACH id IN entries %] <pre>[% Dumper.dump(id) %]</pre> [% END %]
or just [% Dumper.dump(entries) %]
On 18/10/2017 06:28, Gavin Colborne wrote:
Hi Dancers,
I am pretty new to Dancer and really like the light nature of the framework.
I am trying to create a route which will show data from a MongoDB collection and struggling with the syntax.
I have the following in my app:
sub connect_mongo { my $client = MongoDB->connect('mongodb://a.b.c.d') or die "Error connecting to Mongo"; my $db = $client->get_database( 'lfi-perl' ); return($db); }
get '/mongo' => sub { my $db = connect_mongo(); my $docs = $db->get_collection( 'Test-Collection' ); my $all_docs; template 'show_mongo.tt', { 'entries' => $all_docs = $docs->find(), }; };
Then in my view the following:
[% FOREACH id IN entries.keys.nsort %]
[% entries.$id._id %]
I am not able to see any data in my view but am also not seeing any errors.
Any examples of getting data from Mongo in Dancer would be really helpful.
Thank you in advance,
Gavin
Best Regards,
Gavin Colborne
Managing Director,
Tele: 0207 193 2014 Mobile: 0788 400 4339 Skype: gavincolborne Visit: www.littleforest.co.uk
[image: Little Forest LFi] <http://www.littleforest.co.uk/>
_______________________________________________ dancer-users mailing listdancer-users@dancer.pmhttp://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-- Richard Jones
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-- C. Chad Wallace, B.Sc. The Lodging Company http://www.lodgingcompany.com/ OpenPGP Public Key ID: 0x262208A0
Gentlemen, I can't thank you enough. Its all working perfectly :) Hope I can help some others to start learning this neat framework too! Best Regards, Gavin Colborne Managing Director, Tele: 0207 193 2014 Mobile: 0788 400 4339 Skype: gavincolborne Visit: www.littleforest.co.uk [image: Little Forest LFi] <http://www.littleforest.co.uk/> On 19 October 2017 at 18:23, Chad Wallace <cwallace@lodgingcompany.com> wrote:
On Thu, 19 Oct 2017 07:22:46 +0100 Gavin Colborne <gavin@littleforest.co.uk> wrote:
Thanks Richard and everyone,
I added the ".item" so my view shows: [% entries.$id.item('time') %] I still don't see any results :(
I now see in my terminal the following: Argument "client" isn't numeric in numeric comparison (<=>) Which from what I can tell from Gabor's great site https://perlmaven.com/argument-isnt-numeric-in-numeric is some numeric operation problem.
That's because you're using "entries.keys.nsort". If you change that to "entries.keys.sort" you won't get the warning. If "client" is a key, you don't want a numeric sort.
Do you have any examples of controllers passing Mongo data to views so I can compare?
The "$docs->find()" call returns a cursor object. You can read about that in the MongoDB::Cursor man page. The last thing you want to do is iterate over its keys.
If you use the "all" method on the cursor, it'll return an array of documents. So your "entries" key in the template vars hash could be done like this:
template 'show_mongo.tt', { 'entries' => $all_docs = [ $docs->find()->all ], };
And then in the view:
[% FOREACH doc IN entries %] [% doc.time %] [% END %]
Another way to do it would be to use the cursor directly in the template (in case you have a humongous dataset). So your controller would pass the cursor to the template like this:
template 'show_mongo.tt', { 'cursor' => $docs->find(), };
and in the view:
[% WHILE cursor.has_next; SET doc = cursor.next %] [% doc.time %] [% END %]
Then you don't need to store the entire dataset in memory. You'll only look at one document at a time.
My code so far - perhaps the controller is the issue?
sub connect_mongo { my $client = MongoDB->connect('mongodb://a.b.c.d') or die "Error connecting to Mongo"; my $db = $client->get_database( 'lfi-perl' ); return($db); }
get '/mongo' => sub { my $db = connect_mongo(); my $docs = $db->get_collection( 'Test-Collection' ); my $all_docs; template 'show_mongo.tt', { 'entries' => $all_docs = $docs->find(), }; };
Appreciate your help,
Gavin
Best Regards,
Gavin Colborne
Managing Director,
Tele: 0207 193 2014 Mobile: 0788 400 4339 Skype: gavincolborne Visit: www.littleforest.co.uk
[image: Little Forest LFi] <http://www.littleforest.co.uk/>
On 18 October 2017 at 10:13, Richard Jones <ra.jones@dpw.clara.co.uk> wrote:
I've been caught by that before. Try [% entries.$id.item('_id') %]. For some reason (would interested to learn what), TT doesn't seem to be able to cope with leading underscores in data structures. You can prove entries contains data using: [% USE Dumper(Indent=1) %] [% FOREACH id IN entries %] <pre>[% Dumper.dump(id) %]</pre> [% END %]
or just [% Dumper.dump(entries) %]
On 18/10/2017 06:28, Gavin Colborne wrote:
Hi Dancers,
I am pretty new to Dancer and really like the light nature of the framework.
I am trying to create a route which will show data from a MongoDB collection and struggling with the syntax.
I have the following in my app:
sub connect_mongo { my $client = MongoDB->connect('mongodb://a.b.c.d') or die "Error connecting to Mongo"; my $db = $client->get_database( 'lfi-perl' ); return($db); }
get '/mongo' => sub { my $db = connect_mongo(); my $docs = $db->get_collection( 'Test-Collection' ); my $all_docs; template 'show_mongo.tt', { 'entries' => $all_docs = $docs->find(), }; };
Then in my view the following:
[% FOREACH id IN entries.keys.nsort %]
[% entries.$id._id %]
I am not able to see any data in my view but am also not seeing any errors.
Any examples of getting data from Mongo in Dancer would be really helpful.
Thank you in advance,
Gavin
Best Regards,
Gavin Colborne
Managing Director,
Tele: 0207 193 2014 Mobile: 0788 400 4339 Skype: gavincolborne Visit: www.littleforest.co.uk
[image: Little Forest LFi] <http://www.littleforest.co.uk/>
_______________________________________________ dancer-users mailing listdancer-users@dancer.pmhttp://lists.preshweb.co.uk/ mailman/listinfo/dancer-users
-- Richard Jones
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
--
C. Chad Wallace, B.Sc. The Lodging Company http://www.lodgingcompany.com/ OpenPGP Public Key ID: 0x262208A0
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
participants (6)
-
Chad Wallace -
Clive Eisen -
Gavin Colborne -
Richard Jones -
Russell Jenkins -
Scott H