The following article describes Attribute Types in MongoDB.
In particular, MongoDB, documents can have different types of attributes. To emphasize, the attributes in MongoDB belong to one of these types.
- Scalar Types. In effect, these attribute types allow a single value.
- String: Represented as a UTF-8 string.
- Integer: Represented as a 32-bit or 64-bit integer.
- Double: Represented as a floating-point number.
- Boolean: Represented as a true or false value.
- Date: Represented as an ISO date string or as a BSON date type.
- Null: Represented as a null value.
- Array Types. In contrast, arrays represent a collection of values.
- Array: An ordered list of values of any data type, including other arrays.
- Embedded Documents. Meanwhile, a document can be a part of another document.
- Document: A nested set of key-value pairs within a parent document.
- Special Types. However there are some special types.
- Object ID: A unique identifier for each document, automatically generated by MongoDB.
- Binary Data: Binary data can be stored in a MongoDB document as a BSON binary type.
- Decimal128: A 128-bit decimal value.
In addition, it is possible that the structure of a document can change dynamically. So, the user can store documents with different attributes in the same collection.
Attribute Types in MongoDB – Using Arrays
In order to use an array, we need to use square brackets, enclosing the values. For example, the following is an array in a MongoDB document.
{
"_id": ObjectId("5f9d80f0efa12e085f5e5b5c"),
"name": "John Doe",
"email": "johndoe@example.com",
"interests": ["reading", "traveling", "music"],
"purchases": [
{
"product": "Book",
"price": 19.99,
"date": ISODate("2022-01-01T00:00:00.000Z")
},
{
"product": "Airline Ticket",
"price": 299.99,
"date": ISODate("2022-06-01T00:00:00.000Z")
}
]
}
Evidently, the interests
field is an array of strings and the purchases
field is an array of embedded documents. The embedded documents in the purchases
array each have three fields: product
, price
, and date
.
Useful Links
Further Reading
Some Examples of MongoDB Documents
Creating Single Page Applications with Angular
Angular 10 Data Binding in Different Ways
Creating Some Angular Components
Examples of Array Functions in PHP
- AI
- Android
- Angular
- ASP.NET
- Augmented Reality
- AWS
- Bioinformatics
- Biometrics
- Blockchain
- Bootstrap
- C
- C#
- C++
- Cloud Computing
- Competitions
- Courses
- CSS
- Cyber Security
- Data Science
- Data Structures and Algorithms
- Datafication
- Deep Learning
- DevOps
- Digital Forensic
- Digital Trust
- Digital Twins
- Django
- Docker
- Dot Net Framework
- Drones
- Elasticsearch
- Extended Reality
- Git
- Go
- HTML
- Image Processing
- IoT
- IT
- Java
- JavaScript
- Kotlin
- Latex
- Machine Learning
- MEAN Stack
- MERN Stack
- Microservices
- MongoDB
- NodeJS
- PHP
- Power Bi
- Projects
- Python
- Quantum Computing
- React
- Robotics
- Rust
- Scratch 3.0
- Shell Script
- Smart City
- Software
- Solidity
- SQL
- SQLite
- TypeScript
- VB.NET
- Virtual Reality
- Web Designing
- WebAssembly
- XML