8.13. Inserting documents into MongoDB: Working with data types in MongoDB
Page 21 | Listen in audio
Before we dive deep into inserting documents into MongoDB, it's important to understand that MongoDB is a document-oriented NoSQL database. Instead of storing data in tables as is done in a relational database model, MongoDB stores data in flexible BSON-type documents, which means that fields can vary from document to document and the data structure can change over time.
8.13. Inserting Documents into MongoDB
To insert documents into a MongoDB collection, we use the insert() method. This method creates a new document in the collection to which the method is applied. The basic syntax of the insert() method is as follows:
db.collection.insert(document)
Where "db" is our database, "collection" is the collection where we want to insert the document and "document" is the document we want to insert. The document is a JSON object that contains field and value pairs.
Working with data types in MongoDB
MongoDB supports many different data types. Data types in MongoDB can be divided into two large groups: BSON types and non-BSON types. BSON types include:
- String: This is a set of Unicode characters.
- Integer: This is an integer. It can be 32-bit or 64-bit depending on the server.
- Boolean: This is a binary data type and can be true or false.
- Double: This is a floating point number.
- Min/Max keys: These are comparison types.
- Arrays: These are sets of values.
- Timestamp: This is a timestamp value. It's the number of seconds since the Unix era.
- Object: This is an embedded document.
- Null: This is a data type that represents a null value or no value.
- Symbol: This is a data type that is similar to a string.
- Date: This is a date value. It is the number of milliseconds since the Unix era.
- Object ID: This is a unique document ID.
- Binary data: This is a binary data type.
- Code: This is a data type that represents JavaScript code.
- Regular expression: This is a data type that represents a regular expression.
Non-BSON data types include the Undefined type, the Pointer type, and a few other options that are less common.
Example of Document Insertion
Let's look at an example of how to insert a document into a MongoDB collection. Suppose we have a collection called "students" and we want to insert a document into this collection. The document we want to insert is the following:
{ "name": "John Doe", "age": 22, "subjects": ["Math", "Physics", "Chemistry"], "attendance": { "total": 100, "attended": 90 } }
We can insert this document into the "students" collection using the insert() method as follows:
db.students.insert({ "name": "John Doe", "age": 22, "subjects": ["Math", "Physics", "Chemistry"], "attendance": { "total": 100, "attended": 90 } });
After executing this command, MongoDB will create a new document in the "students" collection. MongoDB will also add an "_id" field to this document if the document does not have one. This "_id" field is a unique field that MongoDB uses to identify the document.
In summary, inserting documents into MongoDB is a simple task that involves using the insert() method. MongoDB supports many different data types, which makes the database extremely flexible and capable of handling a variety of different data structures.
Now answer the exercise about the content:
What does the insert() method do in MongoDB?
You are right! Congratulations, now go to the next page
You missed! Try again.
Next page of the Free Ebook: