All Products
Search
Document Center

ApsaraDB for MongoDB:MongoDB Model Service

Last Updated:Jun 25, 2026

You can use the Model Service for ApsaraDB for MongoDB to perform vector search and call large models directly within your database. It integrates with MongoDB Search, eliminating the need for a separate vector database or inference service. This article demonstrates a complete workflow: writing plain text → automatically generating a vector index → querying with natural language.

Prerequisites

  • You have created an ApsaraDB for MongoDB replica set or sharded cluster instance of version 8.3 or later. For instructions, see Create Instance.

  • You have installed mongosh (MongoDB Shell) on your local machine.

Enable Search nodes

  1. In the left-side navigation pane of the instance details page, choose AI Capabilities > MongoDB Search.

  2. Click Enable Search Service, select the specifications and number for the Search nodes, and then confirm.

  3. Wait for the Search node status to change to running. This typically takes a few minutes.

After the service is enabled, the instance supports the $search and $vectorSearch aggregation stages, as well as Search index management commands like createSearchIndexes.

Enable the Model Service

  1. In the left-side navigation pane of the instance details page, choose AI Capabilities > Model Service.

  2. Click Activate Now. After the service is enabled, the system assigns a dedicated endpoint to the instance and generates an API Key.

    dds-<instance-id>-aigateway.mongodbaiapp.<region>.rds.aliyuncs.com
Important
  • The full API key is displayed only once upon creation. If it is lost or compromised, regenerate it in the console.

  • The Model Service is only accessible from the internal network. Before making a call, ensure that the client's IP address is added to the instance's IP address whitelist. Otherwise, the request will be rejected. To configure the whitelist, go to Data Security > IP Address Whitelist Settings on the instance details page.

Create an auto-embedding vector index

Connect to the instance. You can find the connection information on the Database Connection page in the console.

mongosh "mongodb://<user>:<password>@dds-<instance-id>.mongodb.<region>.rds.aliyuncs.com:3717,dds-<instance-id>.mongodb.<region>.rds.aliyuncs.com:3717/admin?replicaSet=mgset-<replica-set-id>"

After connecting, run the following script:

use("mydb");

// 1. Insert sample documents—plain text only, no vector calculation needed
db.articles.insertMany([
  { title: "Cat",  content: "Cats are small, agile mammals often kept as pets" },
  { title: "Dog",  content: "Dogs are loyal companions known for their trainability" },
  { title: "Bird", content: "Most birds can fly, and many migrate across continents" }
]);

// 2. Create an autoEmbed vector index for the content field
//    MongoDB automatically generates vectors using the specified model
db.runCommand({
  createSearchIndexes: "articles",
  indexes: [{
    name: "vector_index",
    type: "vectorSearch",
    definition: {
      fields: [
        { type: "autoEmbed", modality: "text", path: "content", model: "text-embedding-v4" },
        { type: "filter", path: "title" }
      ]
    }
  }]
});

// 3. Check the index status—the index is ready when queryable is true
db.articles.aggregate([{ $listSearchIndexes: {} }]);

For a small dataset, the index is typically created within a few seconds. The index is ready when queryable in the output is true.

Perform semantic search

Once the index is ready, you can query it using natural language. The query text is automatically vectorized by the same model.

db.articles.aggregate([
  {
    $vectorSearch: {
      index: "vector_index",
      path: "content",
      query: "which animals can fly",   // Plain text, not a vector
      numCandidates: 100,
      limit: 3
    }
  },
  { $project: { title: 1, score: { $meta: "vectorSearchScore" } } }
]);

Expected result: The document with the title Bird scores highest because its content is semantically closest to the query "which animals can fly".

Call other models (Optional)

The Model Service also supports other models. Available models include: qwen3.6-flash, qwen3.6-plus, qwen3.5-flash, qwen3.5-plus, and deepseek-v4-pro.

Send a streaming chat request:

curl -X POST \
  http://dds-<instance-id>-aigateway.mongodbaiapp.<region>.rds.aliyuncs.com:8000/v1/chat/completions \
  -H "Authorization: Bearer <api-key>" \
  -H "Content-Type: application/json" \
  -H "Accept: text/event-stream" \
  --no-buffer \
  -d '{
    "model": "qwen3.6-plus",
    "messages": [{"role": "user", "content": "Hello"}],
    "max_tokens": 100,
    "stream": true
  }'

Replace the following placeholders:

Placeholder

Description

<instance-id>

Instance details page in the console

<region>

The region where the instance is deployed, such as cn-beijing

<api-key>

Model Service > API Key Management

Next steps

  • Hybrid search: Combine $search (full-text) and $vectorSearch (semantic) to improve recall.

  • Switch models: Change the model field in the autoEmbed index definition to switch embedding models without needing to manage vector fields manually.