Database Reference
In-Depth Information
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.
Tip
to view the database you are currently working in, simply type db into the MongoDB shell.
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" ] } )
When you define a variable in the shell (for example, document = ( { ... } ) ), the contents of the variable
will be printed out immediately.
Note
> db.media.insert(document)
Line breaks can also be used while typing in the shell. This can be convenient if you are writing a rather lengthy
document, as in this example:
> 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"]
...} )
> db.media.insert(document)
As mentioned, the other option is to insert your data directly through the shell, without defining the document
first. You can do this by invoking the insert function immediately, followed by the document's contents:
> db.media.insert( { "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind" })
 
 
Search WWH ::




Custom Search