YouTube

Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← Getting Data into MongoDB - Data Wranging with MongoDB

Get Embed Code
4 Languages

Showing Revision 2 created 05/25/2016 by Udacity Robot.

  1. Now, after we've gone through and cleaned our data, we're

  2. at a place where we need to get the data
  3. into MongoDB. So, we've looked at queries to MongoDB and
  4. you've done some exercises. Let's look at inserting documents into
  5. collections. Now, the database we've been working with all along
  6. has been the examples database, and we've looked at a
  7. couple of different collections here. We've looked at the autos
  8. collection. We've also done some queries against the cities collection.
  9. Now this script that we're looking at here is actually the
  10. script that I wrote to clean the automobiles data. The way
  11. that I originally wrote this was that it would simply output
  12. JSON documents that I could then import into MongoDB. And that's actually
  13. the, the strategy that I would recommend. But I think it's
  14. also important for you to have an understanding of inserting documents
  15. Into MongoDB. And so, we'll take this opportunity to show you
  16. a little bit about how to do that. Now, there's a lot
  17. about the insert statement that I'm not going to cover
  18. right here. But, this is the simplest form of using
  19. insert. Now, what we're going to do is simply loop through
  20. all of the autos that I've created up here in
  21. the code. Where I've cleaned the data and essentially created
  22. a dictionary for each auto that needs to be inserted
  23. into a MongoDB collection. Then we're going to simply loop
  24. through the list of those autos that I've created and call
  25. the insert statement for each one of them. Passing it
  26. a Python dictionary, PyMongo will translate this into a BSON
  27. encoding and send it across to the database. Now, what
  28. I've got here, is I've just got a couple of print
  29. statements. One that's going to demonstrate how many autos there
  30. are before we do this series of inserts and one that's
  31. going to show how many there are after. Now to
  32. do this, I'm using a command we haven't looked at before,
  33. but I'm sure you can figure out what it does. Simply tells us
  34. how many documents were returned for this
  35. find command. Okay now, let's run this.
  36. Okay. So, what happened was, we went ahead and
  37. inserted all of those documents that we had parsed out
  38. of the CSV file from DBpedia for autos. Then what
  39. I did was check the collection, this myautos collection, before
  40. I did the inserts and got a count of how
  41. many were in it. There were none. After doing all
  42. my inserts, I have close to 7800 documents in this,
  43. myautos collection now. So again, this is an example of
  44. the insert command in MongoDB, one way of getting documents into the database.