mongodbConnect

63 views
Skip to first unread message

Richard Meredith-Hardy

unread,
Oct 18, 2015, 6:16:52 AM10/18/15
to Lucee
MongoDb extension; I'm hoping someone can explain what 'pools connection returned' means.

Function MONGODBCONNECT

Connects to a MongoDB and pools connection returned


What I've been doing is in onApplicationStart()

application.db1 = mongodbConnect("mydb","localhost",27017);

and then using application.db1 in my app as necessary as a sort of datasource, eg:

application.db1["coll1"].insert(a_struct_I_made_earlier);

This is all fine (so far, only a tiny db), but if I do a dump of the application scope I see db1 is a representation of the entire db1 database which is obviously not good.  

I suppose the simplest example is <cfdump var='#mongodbConnect("mydb","localhost",27017)#')>

How am I supposed to be doing this?

Thanks

Richard




Jon Clausen

unread,
Oct 18, 2015, 9:48:18 AM10/18/15
to lu...@googlegroups.com

No, that’s how the extension is set up. If you dump the connections, without a “top” argument, you’re going to see the full representation, including all of your collections, as a struct. If you dump the metadata of the connection, though, you’ll see it’s actually a native class “org.lucee.mongodb.DBImpl”. You’ll be able to see the methods that way without the dump normalizing the entire database.

Once you’ve created your connection, though, should always perform your operations on the collections (use these in your Models):

var myCollection = application.db1.getCollection("col1");

myCollection.insert({"test":"123"});
var cursor = myCollection.find();
while(cursor.hasNext()){
    var document = cursor.next();
    //do stuff here
}

--
See Lucee at CFCamp Oct 22 & 23 2015 @ Munich Airport, Germany - Get your ticket NOW - http://www.cfcamp.org/
---
You received this message because you are subscribed to the Google Groups "Lucee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lucee+un...@googlegroups.com.
To post to this group, send email to lu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lucee/0ac2b671-9537-46bf-a9e1-6b0131fdc857%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Richard Meredith-Hardy

unread,
Oct 18, 2015, 12:26:58 PM10/18/15
to Lucee
hmm

I think what you're saying is that if you do this:  

application.db1 = mongodbConnect("mydb","localhost",27017);

the db isn't actually sitting there in the application scope as a giant struct containing the entire db, it only appears like that if you dump it.

So in fact what I'm doing is OK?

Seems that 
var myCollection = application.db1.getCollection("col1");
is the same as
var myCollection = application.db1["col1"];
so you can do
application.db1["coll1"].insert(a_struct_I_made_earlier);
or
application.db1.getCollection("col1").insert(a_struct_I_made_earlier);
and it's all the same thing. 

Or am I missing the point?

thanks

Richard

Jon Clausen

unread,
Oct 18, 2015, 12:50:37 PM10/18/15
to lu...@googlegroups.com
That's correct.  It's only showing it if you dump it.  Same sort of thing happens when you dump an ORM entity with complex relationships - which is why you should never dump one without a "top" argument.

Using struct key notation for your collections  works as well.  Just a matter of preference.

Jon

Reply all
Reply to author
Forward
0 new messages