init_mongodb_to_postgres

Function init_mongodb_to_postgres 

Source
pub async fn init_mongodb_to_postgres(
    mongo_url: &str,
    target_url: &str,
) -> Result<()>
Expand description

Initial replication from MongoDB to PostgreSQL

Performs one-time migration of MongoDB database to PostgreSQL target using JSONB storage:

  1. Validates MongoDB connection string
  2. Connects to MongoDB and verifies connection
  3. Extracts database name from connection string
  4. Lists all collections from MongoDB database
  5. For each collection:
    • Converts documents to JSONB format
    • Creates JSONB table in PostgreSQL
    • Batch inserts all data

All MongoDB data is stored as JSONB with metadata:

  • id: Original _id field (ObjectId → hex, String/Int → string)
  • data: Complete document as JSON object
  • _source_type: “mongodb”
  • _migrated_at: Timestamp of migration

§Arguments

  • mongo_url - MongoDB connection string (mongodb:// or mongodb+srv://)
  • target_url - PostgreSQL connection string for target (Seren) database

§Returns

Returns Ok(()) if migration completes successfully.

§Errors

This function will return an error if:

  • MongoDB connection string is invalid
  • Cannot connect to MongoDB database
  • Database name is not specified in connection string
  • Cannot connect to target PostgreSQL database
  • Collection conversion fails
  • Database creation or insert operations fail

§Examples

init_mongodb_to_postgres(
    "mongodb://localhost:27017/mydb",
    "postgresql://user:pass@seren.example.com/targetdb"
).await?;