Datatypes
MongoDB supports a variety of data types, which allows for the storage of diverse kinds of information. Understanding these data types is crucial for effective schema design and data manipulation. Here's a rundown of the different data types supported by MongoDB:
Standard Data Types
String
Represents UTF-8 character strings. Strings in MongoDB must be valid UTF-8.
{ "name": "John" }
Integer
Used to store numeric values. MongoDB supports 32-bit and 64-bit integers.
{ "age": 30 }
- Normal integers (int32) maximum value is
+-2,147,483,647
- Long integers (int64) maximum value is
+-9,223,372,036,854,775,807
Boolean
Stores a Boolean value (true
or false
).
{ "isMarried": false }
Double
Used to store floating-point numbers.
{ "score": 99.5 }
Array
An ordered list of values. An array can contain values of different types, including other arrays.
{ "hobbies": ["reading", "swimming"] }
Object
Embedded documents, which are essentially sub-documents within a document.
{ "address": { "street": "123 Main St", "city": "Anytown" } }
Special Data Types
ObjectId
A 12-byte identifier typically used as a unique identifier for documents within a collection.
{ "_id": ObjectId("5f50c31b1c4ae0a63c8b9a0d") }
Date
Used to store the current date or time in UNIX time format. Dates are stored as a signed 64-bit integer representing milliseconds.
{ "createdAt": ISODate("2022-01-01T00:00:00Z") }
Null
Represents a null value.
{ "middleName": null }
Binary Data
Used to store binary data.
{ "data": BinData(0, "1234ABCD") }
Code
Used to store JavaScript code in the document.
{ "logic": Code("function() { return true; }") }
Regular Expression
Stores regular expression patterns.
{ "pattern": /abc/ }
Timestamp
A BSON timestamp type for internal MongoDB use, typically used for replication.
{ "time": Timestamp(161, 1) }
Decimal128
128-bit decimal-based floating-point values capable of emulating decimal rounding with exact precision. Useful for financial calculations.
{ "price": Decimal128("999.99") }
Min/Max keys
Special types used to compare values against the lowest and highest BSON elements.
{ "min": MinKey(), "max": MaxKey() }
JSON vs BSON
JSON (JavaScript Object Notation) and BSON (Binary JSON) are both formats used to represent structured data. While they share some similarities due to BSON being a binary representation of JSON-like documents, there are also key differences between the two. Here's a breakdown:
Similarities
Data Structure: Both JSON and BSON are used to store and exchange data in a structured format. They can represent simple data types like numbers and strings, as well as more complex types like arrays and objects.
Human-Readable: While BSON is binary, it can be easily converted to a human-readable JSON representation, making it easier to debug and understand the data.
Nested Structures: Both formats support nested structures, allowing for complex data models that can include arrays and sub-documents.
Field-Value Pairs: Both JSON and BSON use a field-value pair structure for data representation.
Differences
Binary vs. Textual: BSON is a binary format, whereas JSON is a textual format. This makes BSON more efficient for encoding and decoding, but it also means you can't view BSON data in a text editor without converting it to JSON or another human-readable format.
Data Types: JSON supports basic data types like numbers, strings, booleans, arrays, and objects. BSON extends this by supporting additional types like Date, ObjectId, and Binary data, among others.
Size: BSON data is generally larger than JSON data because it includes additional information like length prefixes and type specifiers. However, BSON also supports data compression techniques, which can mitigate this.
Encoding and Decoding Speed: BSON is designed to be efficiently scanned, parsed, and generated. This makes it faster to encode and decode compared to JSON, which needs to be parsed and serialized.
Field Ordering: In JSON, the order in which fields are added to an object is not guaranteed to be preserved. BSON, on the other hand, maintains field ordering.
Query Support: BSON is used as the data storage and network transfer format for MongoDB. It supports rich and complex queries, including nested and array queries, which are not natively supported in JSON.
Language Support: While JSON is natively supported by many programming languages, BSON requires special libraries for encoding and decoding.
Streaming Support: BSON supports a streaming encoder and decoder, allowing for more efficient processing of large data sets.
Metadata: BSON can include additional metadata that is not part of the JSON data model, such as explicit length information.
Geospatial data types
Geospatial data in MongoDB allows you to perform queries based on geographic location, shapes, and proximity. This feature is particularly useful for applications that require location-based services, such as mapping apps, real estate platforms, and delivery services. MongoDB provides a variety of geospatial query operators and index types to support these use-cases.
Types of Geospatial Data
- Point: Represents a single point on a map, defined by longitude and latitude.
- LineString: Represents a series of connected points.
- Polygon: Represents a closed shape with three or more points.
Geospatial Indexes
- 2d Index: For data stored as legacy coordinate pairs. Useful for simple flat-earth geometry calculations.
- 2dsphere Index: For data stored in GeoJSON format. Supports queries that take into account the spherical nature of the Earth.
Creating Geospatial Indexes
To perform geospatial queries, you first need to create an appropriate index on the field containing the geospatial data.
2d Index
db.collection.createIndex({ location: "2d" });
2dsphere Index
db.collection.createIndex({ location: "2dsphere" });
Geospatial Query Operators
$near
: Returns documents closest to a specific point, sorted by distance.$geoWithin
: Returns documents within a given geometric boundary.$geoIntersects
: Returns documents that intersect with a given geometry.$nearSphere
: Like$near
, but for spherical geometries.
Example Queries
Find Locations Near a Point
db.collection.find({
location: {
$near: {
$geometry: { type: "Point", coordinates: [longitude, latitude] },
$maxDistance: 1000
}
}
});
Find Locations Within a Polygon
db.collection.find({
location: {
$geoWithin: {
$geometry: {
type: "Polygon",
coordinates: [[
[lng1, lat1],
[lng2, lat2],
// ... more points
[lng1, lat1]
]]
}
}
}
});
Considerations
The unit of distance for 2d indexes is the same as the coordinate system of the points. For 2dsphere indexes, it's meters.
Geospatial queries can be resource-intensive. Make sure to test the performance implications for your specific use-case.
MongoDB's geospatial features are not a replacement for a full-fledged GIS (Geographic Information System), but they are quite powerful for many typical use-cases.