Got a YouTube account?

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

English subtitles

← Set Unset - Data Wranging with MongoDB

Get Embed Code
4 Languages

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

  1. OK, so we just looked at a way of
  2. modifying a document using the save method. Now let's
  3. look at an alternative way of updating documents. What
  4. I want to introduce you to here, are two things.
  5. One, the update command or method in pymongo it's
  6. a method on collection objects. And the set operator. Let's
  7. talk about update first. Update expects a query document
  8. as its first parameter, and then as its second parameter,
  9. it expects an update document. And that
  10. update document specifies what operations MongoDB should perform
  11. on the document matching this query. Now,
  12. by default, update operates on just one document.
  13. The first one it finds matching the query document. The semantics of set are, if
  14. this document does not already contain the
  15. fields specified here, then what should happen is
  16. that field should be added with this value.
  17. If the document already contains this field, then that
  18. field will be updated to the value supplied.
  19. Okay, now before I run this code, what I'd
  20. like to do is introduce you to the
  21. inverse operator for set, which is unset. Okay, so
  22. these two pieces of code are almost exactly
  23. the same. The only differences are here and here.
  24. The semantics of unset are, for whatever document matches
  25. this query, if it has the field specified here,
  26. remove it. This value is ignored. Now, if this
  27. document does not already have this field, then this
  28. call has no effect. Okay, so now we've looked
  29. at the set operator and the unset operator, and
  30. their use with the update command. Now, let's go
  31. and run these two programs. And see what their
  32. effect is. But first, I want to query for this
  33. document in the Mongo shell to look at its current state,
  34. and then we can see how running those two programs changes
  35. its current state. So I'm going to run a find one command
  36. here. Now, this is exactly the same command as we
  37. saw in the Python programs we were looking at. The only
  38. difference is in the name. And the names are different merely
  39. to match the conventions of the two different programming languages represented.
  40. In this case, JavaScript because the language of the
  41. Mongo Shell is JavaScript. And, in the other case,
  42. Python, because we're using the Pymongo driver. Okay? So,
  43. going to issue this query. And, I want to point out
  44. that the Iso Code is in fact present. Now
  45. let's run the unset example and then go back
  46. to the Mongo shell and issue our query again.
  47. Okay? And note that the iso country code has
  48. disappeared because we just ran this command. Okay,
  49. now let's run our set example. And again remember
  50. this simply uses the set operator. Let's do
  51. our query again in the Mongo shell and note
  52. that the iso country code is now in
  53. our document again. Okay, so now we've looked at
  54. two different commands that allow us to modify documents.
  55. The save command, and the update command. And we've
  56. looked at two different operators being used in conjunction
  57. with the update command. Okay, now I feel like it's
  58. necessary before we move on to just give you a
  59. heads up. It's really easy to forget to put an
  60. operator here. Okay? If you issue your command this
  61. way, what's going to happen is this entire document, the
  62. document matching this query, will be replaced so that it
  63. contains the underscore ID field and this field only. So,
  64. essentially, the document will be composed entirely of a
  65. single field. So, always take care to use update correctly.