Parse-server: Is there a way to access MongoDB instance in cloud code.

Created on 21 May 2016  路  11Comments  路  Source: parse-community/parse-server

I wondering is it possible to get a mongodb collection instance in cloud code? On my particular case I would like to run db.collection.aggregate(); It would be nice to be able to have something like

new Parse.Object("SomeClass").getCollection().aggregate();

Before moving to open source we used deployd which had similar functionality;

Most helpful comment

Hi,
What I usually do is:

    const Config = require('../node_modules/parse-server/lib/Config');

    const config = Config.get(Parse.applicationId);
    const mongoAdapter = config.database.adapter;
    await mongoAdapter.connect();
    const collection = await mongoAdapter.database.collection(className);

All 11 comments

Cloud Code can execute any javascript, so you can just use the mongo client directly.

Yes, I know that. But I thought about way of getting already created instance so I don't have to create new inside cloud code with database credentials. But yes, that's an option.

No, thats currently not possible. We are working on making Parse be able to use other databases, and some of those changes will make it easier to share connections between Parse Server and Cloud Code, but that also means that we can't ensure there is always a mongo collection available. You can create a global connection for your cloud code, and use env vars for your DB credentials, then the only issue will be that you have 2 connections instead of 1.

Is there now a way to access the mongo connection from Cloud Code? Right now I need to open a new connection and closing it on every requests when doing a native query. How would I go about to keep a persistent mongo connection in Cloud Code?

your cloud code is required only once, so you can easily maintain your connection.

Ah stupid me... Thanks for the help @flovilmart, works like a charm! :)

Awesome!

Hello @flovilmart ,

I want a MongoDB instance which is already created by Parse Server, is there any way to get that instance?

I tried debugging in Parse Instance on cloud code, but no luck.

Thanks.

there is no trivial way to access the parse-server instance from cloud code nor its internals. Perhaps @Tom-Fox1 or @acinader or @dplewis would like to make it easier.

Hi,
What I usually do is:

    const Config = require('../node_modules/parse-server/lib/Config');

    const config = Config.get(Parse.applicationId);
    const mongoAdapter = config.database.adapter;
    await mongoAdapter.connect();
    const collection = await mongoAdapter.database.collection(className);

@flovilmart Thanks for the explanation.

@oallouch, yes, I guess it will work for the cloud code, thanks. :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

carjo422 picture carjo422  路  3Comments

dcdspace picture dcdspace  路  3Comments

kilabyte picture kilabyte  路  4Comments

mohmagdy picture mohmagdy  路  3Comments

dpaid picture dpaid  路  3Comments