DEV Community

Taki089.Dang
Taki089.Dang

Posted on

Mongoose Or MongoDb Native Driver for AI project

Choosing between MongoDB native driver and Mongoose for a project and storing vector embeddings, and domain knowledge. Here's an analysis to help you decide:

1. Use Case Breakdown

a. Storing Users

  • Structure: Users are structured documents (schemas), making Mongoose a good candidate as it enforces a schema and provides object-oriented abstraction.
  • Relationships: If users need relationships or frequent schema updates, Mongoose simplifies these operations.

b. Vector Embeddings and Domain Knowledge

  • Unstructured Data: Vector embeddings are typically unstructured (e.g., arrays of floats). While Mongoose can handle these, the native driver offers more flexibility for direct manipulation.
  • Performance: If you frequently perform vector similarity searches (e.g., with $vectorSearch in MongoDB Atlas), the native driver gives you direct access to query features.

c. Integration

  • Complex Queries: The native driver excels when you need advanced queries, such as vector similarity searches or custom aggregations.
  • Convenience: Mongoose is convenient for CRUD operations but adds overhead for advanced querying patterns.

2. Recommendations

Option A: Use Mongoose for All

When to Choose:

  • If you value schema validation, middleware hooks, and a higher level of abstraction.
  • If the majority of your operations involve structured documents (e.g., user, product).

How:

  • Define schemas for user, embeddings, and domain knowledge.
  • Use plugins or middleware for advanced functionality.

Option B: Use Native Driver for All

When to Choose:

  • If you need fine-grained control over database operations, especially for vector similarity queries.
  • If you prefer to optimize for performance or avoid Mongoose’s abstraction layer.

How:

  • Write direct queries for each operation.
  • Manage schema-like validations in the application logic.

Option C: Use Hybrid Approach

When to Choose:

  • If data are structured, but vector embeddings require advanced queries.
  • If you want the best of both worlds: Mongoose for structured data and native driver for unstructured or complex queries.

How:

  • Use Mongoose for managing users.
  • Use the native driver for vector embeddings and domain knowledge.

3. Suggested Architecture

Hybrid Approach Example:

UserModule: Use Mongoose to define and manage users schema.
Example Schema:

@Schema()
export class Users extends Document {
  @Prop({ required: true })
  username: string;

  @Prop({ required: true })
  password: string;

  @Prop({ required: true })
  email: string;

  @Prop({ type: Object })
  metadata: Record<string, any>;
}
Enter fullscreen mode Exit fullscreen mode

Vector Embedding Module: Use the native driver for handling embeddings.

Example:

async storeEmbedding(embedding: number[]): Promise<void> {
  const db = this.mongoClient.db('users_db');
  const collection = db.collection('embeddings');
  await collection.insertOne({ vector: embedding });
}
Enter fullscreen mode Exit fullscreen mode

Domain Knowledge Module: If domain knowledge requires full-text or similarity search, stick to the native driver.

4. Key Factors for Decision

Image description

Final Recommendation

  • Use Mongoose for Users Infor: Users Infor benefit from schema validation, relationships, and middleware.
  • Use the Native Driver for Vectors and Domain Knowledge: These typically require advanced querying (e.g., $vectorSearch), which Mongoose isn't optimized for.

This hybrid approach provides a balance between maintainability, performance, and feature requirements.

Top comments (0)