Move user Table aws cognito to MongoDB

362 views Asked by At

i have learned about aws cognito and lambda. Usually, aws cognito store them datas to aws dynamoDB. But, in my project i use mongoDB. so, How to store data from aws cognito to mongoDB?

1

There are 1 answers

0
CyberEternal On BEST ANSWER

If you want to store existing data from the AWS Cognito to MongoDB, you can use AWS SDK.

npm i aws-sdk

import AWS from 'aws-sdk'

const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
const params = {
  UserPoolId: 'STRING_VALUE', /* required */
  AttributesToGet: [
    'STRING_VALUE',
    /* more items */
  ],
  Filter: 'STRING_VALUE',
  Limit: 'NUMBER_VALUE',
  PaginationToken: 'STRING_VALUE'
};
cognitoidentityserviceprovider.listUsers(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

After getting the users, store the list of users to the MongoDB.

This function has pagination. The max limit is 60 (If I'm not mistaken). So if you have more then 60 users, you will create a recursive function for getting all users. For the more information check AWS CognitoDocumentation.

For the future, it will be better, if you store a user to the Cognito, get the Cognito username and Cognito sub from the result, and store user data with append these attributes, to the MongoDB.