# 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:** 1\
**Showing post:** 2

<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!

---

_[View the full topic](https://community.auth0.com/t/how-to-add-users-to-mongodb-when-new-users-sign-in/69173)._
