Return to Video

Removing Documents - Data Wranging with MongoDB

  • 0:00 - 0:02
    Okay so we know how to get data into a
  • 0:02 - 0:05
    MongoDB collection. And do queries against that collection. Now I
  • 0:05 - 0:08
    would like to talk about briefly, is how to remove
  • 0:08 - 0:12
    documents from a collection where that's necessary. So the syntax for
  • 0:12 - 0:16
    removing documents is actually quite similar to the syntax for
  • 0:16 - 0:19
    finding documents. So with find if we want to return all
  • 0:19 - 0:21
    the documents in the collection we simply don't express any
  • 0:21 - 0:26
    parameters to the find function. The same is true with remove
  • 0:26 - 0:28
    if I were to execute this query it would remove
  • 0:28 - 0:31
    one by one all of the cities from this collection.
  • 0:31 - 0:33
    A more efficient way of doing that would simply be
  • 0:33 - 0:37
    to call drop which would remove the entire collection and
  • 0:37 - 0:42
    any meta data associated with it. Such as indexes. Now,
  • 0:42 - 0:45
    we can also remove individual documents or documents matching particular
  • 0:45 - 0:48
    criteria. So, for example, I could remove an individual document
  • 0:48 - 0:51
    with the name matching the value "Chicago." So, let me
  • 0:51 - 0:59
    first do a find and there we see that single city. And now, a remove. And then
  • 0:59 - 1:02
    to find again, and sure enough, that city's gone
  • 1:02 - 1:04
    from this collection. Now let's look at a query
  • 1:04 - 1:06
    that touches more than one document. So in
  • 1:06 - 1:09
    this case, what I want to do is identify all
  • 1:09 - 1:14
    documents that don't actually have a name. Okay, you
  • 1:14 - 1:17
    can see there are several returned, in fact 210
  • 1:17 - 1:20
    documents in this city's collection may seem odd. Don't actually
  • 1:20 - 1:22
    have a name. This is a good example here. We've got
  • 1:22 - 1:26
    a country, but no name for the individual city. Same is
  • 1:26 - 1:29
    true with the rest of these returned. Now, as part of
  • 1:29 - 1:33
    a cleaning pass, for example, we might want to remove all cities
  • 1:33 - 1:36
    from this collection after they've been put into the database, all
  • 1:36 - 1:39
    cities that don't actually have a value for the name field.
  • 1:39 - 1:42
    I can do that by sending this same query document to
  • 1:42 - 1:46
    the remove function. So now, I've removed all of those
  • 1:46 - 1:49
    cities that have no name and if I do a
  • 1:49 - 1:50
    find again I can see that there are now in
  • 1:50 - 1:53
    fact no cites that are missing a name field, so
  • 1:53 - 1:55
    the take away here is that remove works in a
  • 1:55 - 1:58
    way very similar to find, you can specify the document
  • 1:58 - 2:02
    to be removed using a query document that has the
  • 2:02 - 2:06
    same syntax and operators available as does the find function.
Tytuł:
Removing Documents - Data Wranging with MongoDB
Video Language:
English
Team:
Udacity
Projekt:
UD032: Data Wrangling with MongoDB
Duration:
02:07

English subtitles

Revisions Compare revisions