Database Reference
In-Depth Information
Viewing Available Databases and Collections
MongoDB automatically assumes a database needs to be created the moment you save
data to it. It is also case-sensitive. For these reasons, it can be quite tricky to ensure that
you're working in the correct database. Therefore, it's best to view a list of all current
databases available to MongoDB prior to switching to one, in case you forgot the
database's name or its exact spelling. You can do this using the show dbs function:
> show dbs
admin
local
Note that this function will only show a database that already exists. At this stage, the
database does not contain any data yet, so nothing else will be listed. If you want to view
all available collections for your current database, you can use the show collections
function:
> show collections
system.indexes
Note that the system.indexes collection is created automatically the moment data is
saved. This collection contains an index based on the _id key value from the document
just inserted; it also includes any custom-created indexes that you've defined.
to view the database you are currently working in, simply type db into the
MongoDB shell.
Tip
Inserting Data into Collections
One of the most frequently used pieces of functionality you will want to learn about is
how to insert data into your collection. All data is stored in BSON format (which is both
compact and reasonably fast to scan), so you will need to insert the data in BSON format
as well. You can do this in several ways. For example, you can define it first, and then save
it in the collection using the insert function, or you can type the document while using
the insert function on the fly:
> document = ( { "Type" : "Book", "Title" : "Definitive Guide to MongoDB 2nd
ed.,
The", "ISBN" : "978-1-4302-5821-6", "Publisher" : "Apress", "Author": [
"Hows, David", "Plugge, Eelco", "Membrey, Peter", “Hawkins, Tim” ] } )
 
 
Search WWH ::




Custom Search