Expand description
MongoDB connection and utilities.
This crate provides MongoDB connection management with automatic user creation, sharding support, and collection management utilities.
§Features
- Connection Management: Connect to MongoDB with automatic user creation
- Sharding Support: Enable sharding for collections with hashed shard keys
- Collection Management: Create collections with indexes and sharding
- Configuration: Environment-based configuration with prefix support
§Usage
```ignore use qm_mongodb::{DbConfig, DB};
#tokio::main
async fn main() -> anyhow::Result<()> {
let config = DbConfig::new()?;
let db = DB::new(“my-app”, &config).await?;
let collection = db.get().collection::
§Environment Variables
| Variable | Description | Default |
|---|---|---|
MONGODB_HOST | MongoDB host | 127.0.0.1 |
MONGODB_PORT | MongoDB port | 27017 |
MONGODB_USERNAME | Database username | (none) |
MONGODB_PASSWORD | Database password | (none) |
MONGODB_DATABASE | Database name | test |
MONGODB_ROOT_USERNAME | Admin username | (none) |
MONGODB_ROOT_PASSWORD | Admin password | (none) |
MONGODB_ROOT_DATABASE | Admin database | admin |
MONGODB_SHARDED | Enable sharding | false |
Modules§
- action
- Action builder types.
- atlas_
search - Helpers for building Atlas Search aggregation pipelines. Use one of the constructor functions
and chain optional value setters, and then convert to a pipeline stage
Documentviainto_stage. - bson
- BSON, short for Binary JSON, is a binary-encoded serialization of JSON-like documents. Like JSON, BSON supports the embedding of documents and arrays within other documents and arrays. BSON also contains extensions that allow representation of data types that are not part of the JSON spec. For example, BSON has a datetime type and a binary data type.
- change_
stream - Contains the functionality for change streams.
- error
- Contains the
ErrorandResulttypes thatmongodbuses. - event
- Contains the events and functionality for monitoring internal
Clientbehavior. - gridfs
- Contains the functionality for GridFS operations.
- options
- Contains all of the types needed to specify options to MongoDB operations.
- raw_
batch_ cursor - Raw batch cursor API for zero-copy document processing.
- results
- Contains the types of results returned by CRUD operations.
Macros§
- db
- Macro to implement AsRefqm::mongodb::DB for a storage type.
Structs§
- Client
- This is the main entry point for the API. A
Clientis used to connect to a MongoDB cluster. By default, it will monitor the topology of the cluster, keeping track of any changes, such as servers being added or removed. - Client
Session - A MongoDB client session. This struct represents a logical session used for ordering sequential
operations. To create a
ClientSession, callstart_sessionon aClient. - Cluster
Time - Struct modeling a cluster time reported by the server.
- Collection
Collectionis the client-side abstraction of a MongoDB Collection. It can be used to perform collection-level operations such as CRUD operations. ACollectioncan be obtained through aDatabaseby calling eitherDatabase::collectionorDatabase::collection_with_options.- Cursor
- A
Cursorstreams the result of a query. When a query is made, the returnedCursorwill contain the first batch of results from the server; the individual results will then be returned as theCursoris iterated. When the batch is exhausted and if there are more results, theCursorwill fetch the next batch of documents, and so forth until the results are exhausted. Note that because of this batching, additional network I/O may occur on any given call tonext. Because of this, aCursoriterates overResult<T>items rather than simplyTitems. - DB
- MongoDB database connection wrapper.
- Database
Databaseis the client-side abstraction of a MongoDB database. It can be used to perform database-level operations or to obtain handles to specific collections within the database. ADatabasecan only be obtained through aClientby calling eitherClient::databaseorClient::database_with_options.- DbConfig
- MongoDB configuration.
- Index
Model - Specifies the fields and options for an index. For more information, see the documentation.
- Namespace
- A struct modeling the canonical name for a collection in MongoDB.
- Search
Index Model - Specifies the options for a search index.
- Server
Info - A description of the most up-to-date information known about a server. Further details can be found in the Server Discovery and Monitoring specification.
- Session
Cursor - A
SessionCursoris a cursor that was created with aClientSessionthat must be iterated using one. To iterate, useSessionCursor::nextor retrieve aSessionCursorStreamusingSessionCursor::stream: - Session
Cursor Stream - A type that implements
Streamwhich can be used to stream the results of aSessionCursor. Returned fromSessionCursor::stream.
Enums§
- Search
Index Type - Specifies the type of search index.
- Server
Type - Enum representing the possible types of servers that the driver can connect to.
- Topology
Type - The possible types for a topology.
Functions§
- insert_
always_ opts - Create options for upsert (insert or update) operations.
- parse_
vec - Parse a MongoDB cursor into a vector of deserialized documents.
Type Aliases§
- BoxFuture
- A boxed future.