how to dump and restore a database in mongodb

This was from the user shell

  1. Open up the shell
  2. change to the project directory
  3. type mongodump
    • with no parameters, this defaults to the local database on 27017, and the “/dump” directory (a subdirectory of the current directory)
  4. There were several databases in the dump directory
    • get rid of all that you don’t want to restore
    • leaving the admin type databases can cause authorization errors (you can’t overwrite the admin databases of the target, I’m assuming)
    • change the name of the directory that has the bson collections in it to whatever you want the database name to be (I change JMJDev to JMJ)
  5. get the connection string (from atlas for compass it was mongodb+srv://<username>:<password>@cluster2.uhsqe.mongodb.net/JMJ)
  6. run the command as below.
  7. NOTE: cluster names have been modified from original for security purposes

Caroline@CAROL-HP MINGW64 ~/projects/jmjbeta (master)
$ mongodump --uri mongodb+srv://<cm........>:<PASSWORD>@cluster0.uhsqe.mongodb.net/JMJ
2022-03-25T17:24:16.937-0400    writing JMJ.enrollments to dump\JMJ\enrollments.bson
2022-03-25T17:24:17.107-0400    done dumping JMJ.enrollments (385 documents)
2022-03-25T17:24:17.160-0400    writing JMJ.courses to dump\JMJ\courses.bson
2022-03-25T17:24:17.248-0400    done dumping JMJ.courses (59 documents)
2022-03-25T17:24:17.272-0400    writing JMJ.users to dump\JMJ\users.bson
2022-03-25T17:24:17.278-0400    writing JMJ.children to dump\JMJ\children.bson
2022-03-25T17:24:17.282-0400    writing JMJ.teachercourses to dump\JMJ\teachercourses.bson
2022-03-25T17:24:17.286-0400    writing JMJ.families to dump\JMJ\families.bson
2022-03-25T17:24:17.346-0400    done dumping JMJ.teachercourses (65 documents)
2022-03-25T17:24:17.354-0400    done dumping JMJ.families (56 documents)
2022-03-25T17:24:17.369-0400    done dumping JMJ.users (66 documents)
2022-03-25T17:24:17.377-0400    writing JMJ.classes to dump\JMJ\classes.bson
2022-03-25T17:24:17.384-0400    writing JMJ.teachercoursestest to dump\JMJ\teachercoursestest.bson
2022-03-25T17:24:17.401-0400    writing JMJ.teachers to dump\JMJ\teachers.bson
2022-03-25T17:24:17.411-0400    done dumping JMJ.children (245 documents)
2022-03-25T17:24:17.443-0400    writing JMJ.grade_levels to dump\JMJ\grade_levels.bson
2022-03-25T17:24:17.443-0400    done dumping JMJ.classes (50 documents)
2022-03-25T17:24:17.452-0400    done dumping JMJ.teachercoursestest (50 documents)
2022-03-25T17:24:17.467-0400    done dumping JMJ.teachers (27 documents)
2022-03-25T17:24:17.475-0400    writing JMJ.years to dump\JMJ\years.bson
2022-03-25T17:24:17.510-0400    done dumping JMJ.grade_levels (18 documents)
2022-03-25T17:24:17.540-0400    done dumping JMJ.years (5 documents)