|
| 1 | +# MongoDB source (contrib) |
| 2 | + |
| 3 | +## Description |
| 4 | + |
| 5 | +MongoDB data sources are [MongoDB](https://www.mongodb.com/) collections that can be used as a source for feature data. The `MongoDBSource` points at a MongoDB collection and provides the metadata Feast needs to read historical features from the offline store's collection. |
| 6 | + |
| 7 | +## Examples |
| 8 | + |
| 9 | +Defining a MongoDB source: |
| 10 | + |
| 11 | +```python |
| 12 | +from feast.infra.offline_stores.contrib.mongodb_offline_store.mongodb import ( |
| 13 | + MongoDBSource, |
| 14 | +) |
| 15 | + |
| 16 | +driver_stats_source = MongoDBSource( |
| 17 | + name="driver_stats", |
| 18 | + timestamp_field="event_timestamp", |
| 19 | + created_timestamp_column="created_at", |
| 20 | +) |
| 21 | +``` |
| 22 | + |
| 23 | +The `name` field becomes the `feature_view` discriminator stored in every document in the `feature_history` collection. |
| 24 | + |
| 25 | +Configuration options such as `connection_string`, `database`, and `collection` are inherited from the offline store configuration in `feature_store.yaml`. |
| 26 | + |
| 27 | +The full set of configuration options is available [here](https://rtd.feast.dev/en/master/#feast.infra.offline_stores.contrib.mongodb_offline_store.mongodb.MongoDBSource). |
| 28 | + |
| 29 | +## Vector Search |
| 30 | + |
| 31 | +The MongoDB online store supports [Atlas Vector Search](https://www.mongodb.com/docs/atlas/atlas-vector-search/), enabling similarity search over feature embeddings stored in MongoDB Atlas. This is powered by the `$vectorSearch` aggregation stage and requires MongoDB Atlas (or the `mongodb/mongodb-atlas-local` Docker image for local development). |
| 32 | + |
| 33 | +See [PR #6344](https://github.com/feast-dev/feast/pull/6344) for full implementation details. |
| 34 | + |
| 35 | +### Configuration |
| 36 | + |
| 37 | +Enable vector search in your `feature_store.yaml`: |
| 38 | + |
| 39 | +```yaml |
| 40 | +project: my_project |
| 41 | +provider: local |
| 42 | +online_store: |
| 43 | + type: mongodb |
| 44 | + connection_string: mongodb+srv://<user>:<pass>@cluster.mongodb.net |
| 45 | + vector_enabled: true |
| 46 | + similarity: cosine # cosine | euclidean | dotProduct |
| 47 | + vector_index_wait_timeout: 60 # seconds to wait for index to become queryable |
| 48 | + vector_index_wait_poll_interval: 1.0 # seconds between polls |
| 49 | +``` |
| 50 | +
|
| 51 | +### Defining a Feature View with Vector Index |
| 52 | +
|
| 53 | +Mark embedding fields with `vector_index=True` and specify `vector_length`: |
| 54 | + |
| 55 | +```python |
| 56 | +from feast import Entity, FeatureView, Field, FileSource |
| 57 | +from feast.types import Array, Float32, Int64, String |
| 58 | +from datetime import timedelta |
| 59 | +
|
| 60 | +item_embeddings = FeatureView( |
| 61 | + name="item_embeddings", |
| 62 | + entities=[Entity(name="item_id", join_keys=["item_id"])], |
| 63 | + schema=[ |
| 64 | + Field( |
| 65 | + name="embedding", |
| 66 | + dtype=Array(Float32), |
| 67 | + vector_index=True, |
| 68 | + vector_length=384, |
| 69 | + vector_search_metric="cosine", |
| 70 | + ), |
| 71 | + Field(name="title", dtype=String), |
| 72 | + Field(name="item_id", dtype=Int64), |
| 73 | + ], |
| 74 | + source=FileSource(path="items.parquet", timestamp_field="event_timestamp"), |
| 75 | + ttl=timedelta(hours=24), |
| 76 | +) |
| 77 | +``` |
| 78 | + |
| 79 | +When `feast apply` (or `store.update()`) runs with `vector_enabled=True`, Atlas vector search indexes are automatically created for any field with `vector_index=True`. Indexes are also automatically dropped when feature views are removed. |
| 80 | + |
| 81 | +### Retrieving Documents via Vector Search |
| 82 | + |
| 83 | +Use `retrieve_online_documents_v2()` to perform similarity search: |
| 84 | + |
| 85 | +```python |
| 86 | +source = FeatureStore(repo_path=".") |
| 87 | +results = store.retrieve_online_documents_v2( |
| 88 | + config=repo_config, |
| 89 | + table=item_embeddings, |
| 90 | + requested_features=["embedding", "title"], |
| 91 | + embedding=[0.1, 0.2, ...], # query vector |
| 92 | + top_k=5, |
| 93 | +) |
| 94 | +
|
| 95 | +# Each result is a (event_timestamp, entity_key_proto, feature_dict) tuple. |
| 96 | +# feature_dict includes a synthetic "distance" key with the vector search score. |
| 97 | +for ts, entity_key, features in results: |
| 98 | + print(features["title"].string_val, features["distance"].float_val) |
| 99 | +``` |
| 100 | +``` |
| 101 | + |
| 102 | +### How It Works |
| 103 | + |
| 104 | +- **Index creation**: `update()` creates an Atlas vector search index named `<feature_view>__<field>__vs_index` for each vector-indexed field. It waits for the index to reach `READY` status before proceeding. |
| 105 | +- **Query execution**: `retrieve_online_documents_v2()` builds a `$vectorSearch` aggregation pipeline with `numCandidates = max(top_k * 10, 100)` and the specified `limit`. |
| 106 | +- **Score**: Results include a `distance` field populated from `$meta: "vectorSearchScore"`. |
| 107 | +- **BSON compatibility**: Query vectors are coerced to native Python floats to avoid numpy serialization issues. |
| 108 | +- **Idempotency**: Calling `update()` multiple times will not duplicate indexes. |
| 109 | + |
| 110 | +## Supported Types |
| 111 | + |
| 112 | +MongoDB data sources support all eight primitive types (`bytes`, `string`, `int32`, `int64`, `float32`, `float64`, `bool`, `timestamp`) and their corresponding array types. Complex types such as `Map` and `Struct` are preserved through the MongoDB document model. |
| 113 | +For a comparison against other batch data sources, please see [here](overview.md#functionality-matrix). |
0 commit comments