We can also import data from external files such as CSV or CSV or JSON files. So MongoDB comes up with a bunch of tools for these things. For example, if I go to the MongoDB installation folder, there is a file called Mongo input dot e xe. And the Mongo import, as the name suggests, imports a file into a MongoDB collection off a particular database. So if I have to go to a command prompt, and issue a command, Mongo import test does help. It would give me a bunch of options that I can use while importing.
So it says Mongo import, few options, and one file. The data can be represented as CSV comma separated values, or tsp tab separated values or JSON, JavaScript Object Notation format. We can also import if the data is just a collection of MongoDB documents, we'll have a look into each one of these. I have a file called context data dot JSON. And that contains 1000 contact information with similar fields. Now, it is also possible in practice that the fields may vary that we have already seen.
But in this particular case, I have all of them have the same structure. Now, in order to import this, all I have to do is to come to the command prompt and then say Mongo input. And then I have to give you options. The number one option is what kind of data that I have. Now, I have a JSON array. The second important option that I might have to specify is the DB.
In our case, it's called test dB. And in the test DB database, what is the name of the collection to which you want To import, if the collection name that you have given is not available, it gets created. If it's already there, it's going to add these documents into the existing collection. If you don't mention the collection, the name of the file itself will be considered as the collection. Now since the name of the file is context data and already have a collection called contacts, it is good to get the name of the collection. So I'll say dash dash collection.
And then I'll say contacts. And now the last option, which is the name of the file. The part that I'm giving here is a relative path because I'm already in the C colon users we don't folder related to that there is a folder called desktop. And there is a context underscore data dot JSON. So if everything is okay, it should input 1000 documents. And now we can go and have a look at that in our Mongo shell.
So I can just give a command DB dot contract starts say for example I can say count and then they'll give me 1004 because four documents we added earlier and now thousand documents have been inserted through the import command. Now, if I gave it here DB dot context dot find, you will see that I see only 20 documents and then it says here type it for more now, it is a short form for iterate. So, I can now type it and I should see 20 more Alternatively, you can assign the fine functions return value to a variable for example, I can say war data equals to dB dot context dot find. Now, data here is an iterator if you simply press data it is as if you wanted to see the first 20 documents. You can also loop through that or you can also say war contacts equals to array to convert that into an Ira objects now I can say contracts of zero.
And that's the first contracts, contracts of 1000. And that would be the 1,001st to document. And, likewise, I can go and access all data