Import & Export in MongoDB
5K views
Oct 24, 2024
Import & Export in MongoDB Watch more Videos at https://www.tutorialspoint.com/videotutorials/index.htm Lecture By: Mr. Arnab Chakraborty, Tutorials Point India Private Limited
View Video Transcript
0:00
In this video we are discussing import and export in MongoDB
0:05
We know that input means we are getting some data sources from external source to the MongoDB
0:11
and export means from the MongoDB to the external sources. And here for this purpose we require the data sources which will be coming from the external
0:21
must be either in the form of JSON or CSB or TSB
0:26
So this is a common format in which we can do the import and export
0:29
export in MongoDB. So, let us go for further discussion on it. So, MongoDB import and export
0:38
To import data from external source to MongoDB or export data from MongoDB to external
0:44
sources, we should use these operations. Okay. So Mongo supports JSON, CSV and TSB file
0:53
So, CSV means comma separated values where the values will be separated by the commas
0:58
and t sb means tab separated values so comma separated values and tab separated values so this mongo
1:07
supports jason there is a javascript object notation c sb and t sb files so this is our data import and export It can be done in uh for the input we be using this Mongo import and in case of export we be using
1:22
the command that is a Mongo export. So, let us discuss all of them one by one
1:28
So, MongoDB import and export, the Mongo import tool is used to import the contents of extended
1:36
jason, CSV or t sb files into the MongoDB documents. So, from there we'll be importing them to the MongoDB documents
1:48
So the syntax of this tool is that Mongo import minus minus dB then the database
1:54
name, minus, collection, then the collection name, minus minus file, then the file name
2:02
In this way, the respective import will take place. If we want to import JSON array, then we need to specify the JSON array option for the Mongo import command
2:15
So, in the case of JSON array, we are supposed to use this JSON array option in our Mongo import command
2:23
So now let us discuss Mongo export The Mongo export tool is used to export the contents of the MongoDB documents to the external JSON or CSB or TSB file So the syntax for this JSON export is
2:39
that, that is Mongo export, there is a tool name, minus minus DB, then database name
2:46
minus minus collection, then collection name, minus minus out, then file name. So this is the
2:54
respective syntax for jason export so now in case of c sb export the syntax will be like this
3:00
that is a mongo export minus minus db then database name minus minus collection then collection name
3:08
minus minus type is equal to c sb minus fields then the field one field two in this way and then
3:15
minus minus import and the file name dot c sb so in this way you can do the export for the c sb we can do the export for the
3:23
the JSON files. So, now let us go for one practical demonstration where you will be issuing
3:29
such commands to give you a better idea that how these tools are working. In this demonstration
3:35
we are going to show you how to perform the import and export operation on MongoDB So at first we are concentrating on the import operation So here we are opening one terminal using Control Alter T So we have opened one terminal here And in this particular
3:52
terminal, I want to show you that there is one file at the root. So LS minus L at the home path
3:59
we're having one file that is the student dot JSON. So these are respective file we're having at the
4:07
home path. So let me show you the content. So this is the content we are finding that we are having one JSON array and the JSON array
4:17
is having four JSON objects. So we are finding this one with a roll number one, roll number two, roll number three and
4:23
roll number four. So here we're having four JSON objects are there under this JSON array
4:30
So you want to import this JSON file onto a new MongoDB database
4:35
So let the name of the database be new DB. So let me show you that this
4:39
particular database whether it is pre-existing or not so you shall go for pseudo service
4:46
MongoDB start so pseudo service MongoDB start then it is asking for the password
#Programming