Podcast
Questions and Answers
What command is used to switch databases in MongoDB?
What command is used to switch databases in MongoDB?
Which command is used to create a collection in MongoDB?
Which command is used to create a collection in MongoDB?
What is displayed if there are no collections in a database when 'show dbs' command is used?
What is displayed if there are no collections in a database when 'show dbs' command is used?
What kind of data records does MongoDB store documents as?
What kind of data records does MongoDB store documents as?
Signup and view all the answers
What happens if you drop a collection in MongoDB using 'db.Student.drop()'?
What happens if you drop a collection in MongoDB using 'db.Student.drop()'?
Signup and view all the answers
When would MongoDB create a database if it does not exist?
When would MongoDB create a database if it does not exist?
Signup and view all the answers
What is the primary purpose of BSON?
What is the primary purpose of BSON?
Signup and view all the answers
Which field name is reserved for use as a primary key in MongoDB documents?
Which field name is reserved for use as a primary key in MongoDB documents?
Signup and view all the answers
What is the purpose of the command 'db.createCollection("Employee")' in MongoDB?
What is the purpose of the command 'db.createCollection("Employee")' in MongoDB?
Signup and view all the answers
What is the purpose of the command 'db.Student.drop()' in MongoDB?
What is the purpose of the command 'db.Student.drop()' in MongoDB?
Signup and view all the answers
When switching databases in MongoDB using the 'use' statement, how can you verify the current database you are using?
When switching databases in MongoDB using the 'use' statement, how can you verify the current database you are using?
Signup and view all the answers
Study Notes
Switching Database
- In the shell,
db
refers to the current database. - To display the current database, type
db
. - The default database is
test
. - To switch databases, use the
use
command, for example,use seedinfotech
.
Creating a Collection
- To create a collection in the database, use
db.createCollection("CollectionName")
. - For example,
db.createCollection("Employee")
. - If a collection does not exist, MongoDB creates it when you first store data for that collection.
Inserting Data
- To insert data into a collection, use
db.CollectionName.insertOne({document})
. - For example,
db.Student.insertOne({'eid':101, 'name': "Jagruti"})
. - To check collections, use
show collections
. - To show collection of documents, use
db.CollectionName.find()
.
Dropping a Collection or Database
- To drop a collection, use
db.CollectionName.drop()
. - For example,
db.Student.drop()
. - To drop a database, use
db.dropDatabase()
.
MongoDB Documents
- MongoDB stores data records as BSON (Binary Javascript Object Notation) documents.
- BSON is a binary representation of JSON documents, though it contains more data types than JSON.
- Documents are composed of field-and-value pairs.
- The value of a field can be any of the BSON data types, including other documents, arrays, and arrays of documents.
Document Structure
- Documents have the following structure:
{ field1: value1, field2: value2, field3: value3,...fieldN: valueN }
. - Field names are strings.
- Field names have restrictions, such as:
- The field name
_id
is reserved for use as a primary key; its value must be unique in the collection, is immutable, and may be of any type other than an array. - If the
_id
contains subfields, the subfield names cannot begin with a ($) symbol. - Field names cannot contain the null character.
- The server permits storage of field names that contain dots (.) and dollar signs ($).
- The field name
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Learn how to switch databases in MongoDB using the 'use' command. Use 'db' to display the current database and 'show dbs' to view all existing databases. Understand how to create collections within a database.