# Use Mongodb-Connection in Cloud Code

**URL:** <https://community.parseplatform.org/t/use-mongodb-connection-in-cloud-code/1555>\
**Category:** Cloud Code\
**Tags:** mongodb\
**Created:** [April 9, 2021, 9:13pm UTC](https://community.parseplatform.org/t/use-mongodb-connection-in-cloud-code/1555 "2021-04-09T21:13:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![soumas](https://community.parseplatform.org/user_avatar/community.parseplatform.org/soumas/32/509_2.png) [@soumas](https://community.parseplatform.org/u/soumas)\
**Post date:** [April 9, 2021, 9:13pm UTC](https://community.parseplatform.org/t/use-mongodb-connection-in-cloud-code/1555/1 "2021-04-09T21:13:42Z")

</div>

Hi Guys!  
As I have read, it is not possible to put a unique index on a mongodb field with Parse.Schema. Instead, we have to do that right on the mongodb … right?

I have a cloud function “migrate” as a central point to run update scripts. At this point I would also like to carry out this direct intervention in mongodb.

My question now is whether there is a way to access the server option “databaseURI” within a cloud function? Or is there perhaps a “more elegant” way to use the same database connection as parse?

Thank you for your help,  
Thomas

---

<div class="post-metadata">

**Author:** ![dblythy](https://community.parseplatform.org/user_avatar/community.parseplatform.org/dblythy/32/231_2.png) [@dblythy](https://community.parseplatform.org/u/dblythy)\
**Post date:** [April 10, 2021, 7:00am UTC](https://community.parseplatform.org/t/use-mongodb-connection-in-cloud-code/1555/2 "2021-04-10T07:00:40Z")

</div>

Unique indexes will hopefully be supported internally soon - follow [this issue!](https://github.com/parse-community/parse-server/issues/7289)

You can however use this little hack I’ve just written to create unique indexes:

```auto
const Config = require('./../node_modules/parse-server/lib/Config');
const adapter = Config.get('appId').database.adapter;
await adapter.ensureUniqueness(
  'className',
  {
    fields: {
      fieldName: { type: 'String' },
    },
  },
  ['fieldName']
);

```

---

<div class="post-metadata">

**Author:** ![soumas](https://community.parseplatform.org/user_avatar/community.parseplatform.org/soumas/32/509_2.png) [@soumas](https://community.parseplatform.org/u/soumas)\
**Post date:** [April 10, 2021, 2:16pm UTC](https://community.parseplatform.org/t/use-mongodb-connection-in-cloud-code/1555/3 "2021-04-10T14:16:25Z")

</div>

works like a charm - thank you!

---

<div class="post-metadata">

**Author:** ![xeoshow](https://community.parseplatform.org/user_avatar/community.parseplatform.org/xeoshow/32/1458_2.png) [@xeoshow](https://community.parseplatform.org/u/xeoshow)\
**Post date:** [May 6, 2023, 6:46am UTC](https://community.parseplatform.org/t/use-mongodb-connection-in-cloud-code/1555/4 "2023-05-06T06:46:16Z")

</div>

Hello @dblythy ,

Is creating unique indexes supported in the latest 6.1.0? Thanks v much.
