Rocket.Chat icon indicating copy to clipboard operation
Rocket.Chat copied to clipboard

Access Mongo.Collections from custom script

Open jkolhi opened this issue 10 years ago • 8 comments

Your Rocket.Chat version: 0.22.0

Hi, I need to access mongo collections from the custom script. How can I do this? Either from the rocketchat db, or from a separate mongo db. Just looking for opening a collection and reading info.

Kind of stuck currently, all help very much appreciated.

jkolhi avatar Mar 18 '16 09:03 jkolhi

Hi there,

Please have a look at the following documentation:

either from within RC or Meteor: http://docs.meteor.com/#/full/mongo_collection or from outside: http://stackoverflow.com/questions/4837673/how-to-execute-mongo-commands-through-shell-scripts

These two links were the first results when searching with DuckDuckGo - give it a try :)

konsumate avatar Mar 18 '16 09:03 konsumate

Great, thanks! I have been going in that direction, but still can't read the collection I made in mongodb. Must be something very trivial, but it is driving me crazy.

Rocket.chat runs connected to an external mongodb. Works perfect. My code will not access a collection on the same database. Any ideas?

Code below, being run as custom code.

function mongo_test() {
    console.log("--- here we go ---")
    Test = new Mongo.Collection('test')
    result = Test.find({}).fetch()


```    console.log(JSON.stringify(result,null,4))
    console.log("--- done ---")
    return result;
}`
I created a new collection "test" in the rocket.chat database. 
When code is running, I just get an empty [] result back.  

What am I missing?

jkolhi avatar Mar 18 '16 11:03 jkolhi

How exactly are you adding this custom script? So I can understand the scope it is running.

engelgabriel avatar Mar 18 '16 13:03 engelgabriel

I added a new template method as a local change and made a call from room.html/room.coffee. The function gets called, but the result is just empty. I have started to think that this can be a publish/subscribe thing, but not sure how to add this in a simple fashion. (Have not worked with Meteor or Mongodb earlier)

jkolhi avatar Mar 18 '16 13:03 jkolhi

Ideally you should not add functionality directly into the main code (unless you plan to do a PR) as it will make harder for you to keep up with the updates (making merges forever). You can add pretty much any functionally via packages, or if any API is missing for something you need, just let us know.

Having said that, are you running that code on the server? There is always a server and a client side: https://www.meteor.com/tutorials/blaze/publish-and-subscribe

I'd ask you to close this issue as we use this as a bug/feature tracker. Please join the https://demo.rocket.chat/channel/contributors channel for general development enquires.

engelgabriel avatar Mar 18 '16 13:03 engelgabriel

Where can I find information about how to add a package, it sounds like a good way forward? I am trying to wrap my head around how Rocket.Chat is working but it is a large project and hard to grasp. The main code modification was just to test stuff locally. Sorry for hogging the issue board and thanks for the help, very much appreciated!

jkolhi avatar Mar 18 '16 13:03 jkolhi

The best way to add a package is to look into the /packages folder for an example and create your own from copying one of the folders. Updated /packages/YOUR-PACKAGE-NAME/package.js accordingly and add it to /.meteor/packages

engelgabriel avatar Mar 18 '16 13:03 engelgabriel

Great! That is very good information - I will test it out! Thank you Gabriel!

jkolhi avatar Mar 18 '16 13:03 jkolhi