# How to add users to mongoDB when new user's sign in

**URL:** <https://community.auth0.com/t/how-to-add-users-to-mongodb-when-new-users-sign-in/69173>\
**Category:** Get Help\
**Tags:** mongodb\
**Created:** [September 7, 2021, 7:06am UTC](https://community.auth0.com/t/how-to-add-users-to-mongodb-when-new-users-sign-in/69173 "2021-09-07T07:06:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hussamkhatib](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/hussamkhatib/32/30961_2.png) [@hussamkhatib](https://community.auth0.com/u/hussamkhatib)\
**Post date:** [September 7, 2021, 7:06am UTC](https://community.auth0.com/t/how-to-add-users-to-mongodb-when-new-users-sign-in/69173/1 "2021-09-07T07:06:26Z")

</div>

what is the best way to push data into our database when a user is created in auth0.  
There is something called post registration hook in auth0 pipelines , post registration **actions** and there is SSO integration for mongo db .  
Not sure what to use , Would be useful If you provide some sample code on how It’s done , because I not sure how the inline auth0 editor is connecting to the our database

---

<div class="post-metadata">

**Author:** ![ax0nic](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ax0nic/32/34349_2.png) [@ax0nic](https://community.auth0.com/u/ax0nic)\
**Post date:** [January 12, 2022, 2:10pm UTC](https://community.auth0.com/t/how-to-add-users-to-mongodb-when-new-users-sign-in/69173/2 "2022-01-12T14:10:49Z")

</div>

In case anyone is finding this recently - here is a working snippet. Add this to an Action in Auth0 as described here: [Write Your First Action](https://auth0.com/docs/customize/actions/write-your-first-action)

```auto
/**
 * @param {Event} event - Details about newly created user.
 */

const { MongoClient } = require("mongodb");

exports.onExecutePostUserRegistration = async (event) => {
  const newUser = { sub: event.user.sub };
  const client = await MongoClient.connect(event.secrets.MONGO_DB);
  const db = client.db();
  await db.collection("users").insertOne(newUser);
  client.close();
};

```

The user registration process will create an entry in your MongoDB database.

Cheers!

---

<div class="post-metadata">

**Author:** ![stoplion](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/stoplion/32/42814_2.png) [@stoplion](https://community.auth0.com/u/stoplion)\
**Post date:** [January 19, 2023, 8:41pm UTC](https://community.auth0.com/t/how-to-add-users-to-mongodb-when-new-users-sign-in/69173/3 "2023-01-19T20:41:45Z")

</div>

> [@ax0nic](#):
>
> ```auto
> const { MongoClient } = require("mongodb");
> 
> exports.onExecutePostUserRegistration = async (event) => {
> const newUser = { sub: event.user.sub };
> const client = await MongoClient.connect(event.secrets.MONGO_DB);
> const db = client.db();
> await db.collection("users").insertOne(newUser);
> client.close();
> };
> 
> ```

Where do you put env vars in Auth0?

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [January 19, 2023, 9:33pm UTC](https://community.auth0.com/t/how-to-add-users-to-mongodb-when-new-users-sign-in/69173/4 "2023-01-19T21:33:29Z")

</div>

A post was merged into an existing topic: [Environment variables in Actions?](https://community.auth0.com/t/environment-variables-in-actions/98523/5)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [February 2, 2023, 9:34pm UTC](https://community.auth0.com/t/how-to-add-users-to-mongodb-when-new-users-sign-in/69173/5 "2023-02-02T21:34:24Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
