MongoDB Experience: Gotcha with collection names

In my earlier tests I bulk loaded data with the following command.

mongoimport -d olympics -c olympic_event -type tsv --headerline -f name,id,sport,demonstration_competitions,olympic_games_contested,competitions,contested_as_demonstration_event --drop olympic_event.tsv
connected to: 127.0.0.1
dropping: olympics.olympic_event
imported 775 objects

As you can see I imported 775 objects, however when I went to review them via the mongo interactive shell I found no data.

> use olympics;
switched to db olympics
> db.olypics.olympic_event.find();
# No results?

I was able to confirm these objects were in the namespace.

> db.system.namespaces.find();
{ "name" : "olympics.system.indexes" }
{ "name" : "olympics.demonstration_event_athlete_relationship" }
{ "name" : "olympics.demonstration_event_athlete_relationship.$_id_" }
{ "name" : "olympics.olympic_athlete" }
{ "name" : "olympics.olympic_athlete.$_id_" }
{ "name" : "olympics.olympic_athlete_affiliation" }
{ "name" : "olympics.olympic_athlete_affiliation.$_id_" }
{ "name" : "olympics.olympic_bidding_city" }
{ "name" : "olympics.olympic_bidding_city.$_id_" }
{ "name" : "olympics.olympic_city_bid" }
{ "name" : "olympics.olympic_city_bid.$_id_" }
{ "name" : "olympics.olympic_demonstration_competition" }
{ "name" : "olympics.olympic_demonstration_competition.$_id_" }
{ "name" : "olympics.olympic_demonstration_medal_honor" }
{ "name" : "olympics.olympic_demonstration_medal_honor.$_id_" }
{ "name" : "olympics.olympic_event" }
{ "name" : "olympics.olympic_event.$_id_" }
{ "name" : "olympics.olympic_event_competition" }
{ "name" : "olympics.olympic_event_competition.$_id_" }
{ "name" : "olympics.olympic_games" }
has more
> it
{ "name" : "olympics.olympic_games.$_id_" }
{ "name" : "olympics.olympic_host_city" }
{ "name" : "olympics.olympic_host_city.$_id_" }
{ "name" : "olympics.olympic_mascot" }
{ "name" : "olympics.olympic_mascot.$_id_" }
{ "name" : "olympics.olympic_medal" }
{ "name" : "olympics.olympic_medal.$_id_" }
{ "name" : "olympics.olympic_medal_demonstration" }
{ "name" : "olympics.olympic_medal_demonstration.$_id_" }
{ "name" : "olympics.olympic_medal_honor" }
{ "name" : "olympics.olympic_medal_honor.$_id_" }
{ "name" : "olympics.olympic_participating_country" }
{ "name" : "olympics.olympic_participating_country.$_id_" }
{ "name" : "olympics.olympic_sport" }
{ "name" : "olympics.olympic_sport.$_id_" }
{ "name" : "olympics.olympic_venue" }
{ "name" : "olympics.olympic_venue.$_id_" }

The problem is I was using the namespace object with db.find(), not the collection object. I am already in the database scope with the use command.

Knowing this I get what I expected with the correct collection name.

> db.olympic_event.find();
{ "_id" : ObjectId("4c0fb666a5cd86585be7c0fd"), "name" : "Men's Boxing, Super Heavyweight +91kg", "id" : "/guid/9202a8c04000641f8000000008d88df9", "sport" : "Boxing", "demonstration_competitions" : "", "olympic_games_contested" : "2008 Summer Olympics,1984 Summer Olympics,2000 Summer Olympics,2004 Summer Olympics,1988 Summer Olympics,1996 Summer Olympics,1992 Summer Olympics", "competitions" : "Boxing at the 1984 Summer Olympics - Super Heavyweight ,Boxing at the 2000 Summer Olympics - Super Heavyweight,Boxing at the 1988 Summer Olympics - Super Heavyweight ,Boxing at the 2004 Summer Olympics - Super Heavyweight,Boxing at the 1992 Summer Olympics - Super Heavyweight ,Boxing at the 2008 Summer Olympics - Super heavyweight,Boxing at the 1996 Summer Olympics - Super Heavyweight" }
{ "_id" : ObjectId("4c0fb666a5cd86585be7c0fe"), "name" : "Men's Judo, 60 - 66kg (half-lightweight)", "id" : "/guid/9202a8c04000641f8000000008d88d0e", "sport" : "Judo", "demonstration_competitions" : "", "olympic_games_contested" : "2004 Summer Olympics,2000 Summer Olympics,2008 Summer Olympics", "competitions" : "Judo at the 2008 Summer Olympics – Men's Half Lightweight (66 kg),Judo at the 2000 Summer Olympics - Men's Half Lightweight (66 kg),Judo at the 2004 Summer Olympics - Men's Half Lightweight (66 kg)" }
{ "_id" : ObjectId("4c0fb666a5cd86585be7c0ff"), "name" : "Men's Tennis, Indoor Singles", "id" : "/guid/9202a8c04000641f8000000010be70e8", "sport" : "Tennis", "demonstration_competitions" : "", "olympic_games_contested" : "1912 Summer Olympics,1908 Summer Olympics", "competitions" : "Tennis at the 1908 Summer Olympics - Men's Indoor Singles,Tennis at the 1912 Summer Olympics - Men's Indoor Singles" }
...

It’s interesting that a collection name can contain a fullstop ‘.’ which is the delimiter in the command syntax. In my earlier observation I was not getting an error, only an empty response. For example you can do this.

> use test;
switched to db test
> db.foo.x.save({a: 1});
> db.foo.x.find();
{ "_id" : ObjectId("4c0fc1784ff83a6831364d57"), "a" : 1 }
Tagged with: Mongodb

Related Posts

MongoDB Experience: What’s running in the DB

You can very easily find out the running threads in the database (e.g. like a MySQL SHOW PROCESSLIST) with db.currentOp . > db.currentOp(); { "inprog" : [ ] } No much happening, however under some load you can see

Read more

MongoDB Experience: Bulk Loading Data

mongoDB has a mongoimport command. The docs only shows the usage but not any examples. here are my first examples. data1.csv 1 2 3 4 5 6 7 8 9 0 You need to specify your database (-d) and collection (-c) for importing.

Read more

MongoDB Experience: Online Resources

Following the initial Quickstart docs you will find a lot of online documentation. The following are good places to start. Tutorial The Interactive Shell Manual Admin Zone Starting and Stopping Monitoring and Diagnostics Backups There is also a Getting Started however I found this a duplication of the Quickstart.

Read more