Chronos/DBWaitlist

Blog / AI agents

How to let an AI agent write to MongoDB safely

The agent works in a branch of a copy of your collections; each approved change is one multi-document transaction that sends only the fields that changed. What it needs, and what is not there yet.

Abhishek DG · 4 min read
  • AI agents
  • MongoDB
  • How-to

Let it work on a copy of your collections, in its own branch, and write each approved change back in one multi-document transaction that sends only the fields that changed. Chronos DB does this beside MongoDB 5.0 or later. The agent never holds a MongoDB credential, and a document someone else changed in the meantime is never overwritten.

What does it need?

  • A replica set. MongoDB has transactions only on a replica set, and Atlas is one. A standalone mongod is refused at attach, with nothing copied.
  • MongoDB 5.0 or later.
  • A user with readWrite on the database. Chronos keeps two collections of its own there, chronos_merges and chronos_merge_rows.
terminal
export CHRONOS_SOURCE_URL='mongodb://chronos:example-password@db.internal:27017/shop?replicaSet=rs0&tls=true'chronos attach shop --tables customers,orders --fields org_idexport CHRONOS_TOKEN=$(openssl rand -hex 24)         # the guest'sexport CHRONOS_ADMIN_TOKEN=$(openssl rand -hex 24)   # yourschronos serve shop

How does an agent see a collection?

As a table. A document is a row with three kinds of column: _id, one column for each field you name with --fields, and doc, the rest of the document as jsonb.

SQL, in the agent's world
select doc->>'name' from customers where org_id = 42;update customers set doc = jsonb_set(doc, '{owner}', '"ada"') where _id = '65f1a2b3c4d5e6f708192a3b';insert into customers (org_id, doc) values (42, '{"name": "NewCo"}');   -- _id is a new ObjectId

Name in --fields the fields an agent filters by, such as the tenant’s id. A filter on a named field reads only the matching documents; a filter on a field inside doc counts as reading the whole collection, which matters when you ask Chronos to check what a run read.

What does a merge send to MongoDB?

Only what changed. An update goes as $set and $unset by path, never as the whole document, so fields the agent did not touch keep their order and their types.

  • Starta transaction opens; the folder’s marker is checkedthe right database, or it stops
  • Compareevery document the merge writes is read and comparedany difference: conflict, nothing written
  • Writeinserts and updates, then deletesonly changed fields, by path
  • Commitdocuments are read back and the merge is recordedit can be undone by its id
One multi-document transaction. A document that differs from Chronos’s copy is a conflict, and then nothing at all is written.

How does the copy stay up to date?

chronos refresh reads every document. To follow changes as they happen, start the server with --follow: it reads a change stream on each attached collection, learns which documents changed, and takes just those.

terminal
chronos serve shop --follow

If the server was away longer than MongoDB’s oplog keeps, it says so and starts again with a refresh.

Can a merge be undone?

Yes, by its id, for 30 days. Each write-back keeps the documents it wrote, before and after, in its own transaction, and the undo compares each document before putting it back.

What is not there, and not tested?

Not yetWhat happens instead
Sharded clustersrefused at attach: use a replica set
Views, time-series and capped collectionsrefused: attach the collections a view reads
Array elements changed one by onea changed array is written whole
Rehearsing a merge and rolling it backa dry run, which writes nothing

If your own product’s agents work on MongoDB, Chronos Gateway adds MongoDB’s own tools (find, count, insert, update_one and the rest) on top of the same branches. Every detail of attach: Beside MongoDB.

Questions

Can an AI agent write to MongoDB without direct access?

Yes. With Chronos DB attached to a MongoDB replica set, the agent works in a branch of a copy of your collections and holds no MongoDB credential. Each approved merge is written in one multi-document transaction after every document is compared.

Does a merge overwrite the whole document?

No. An update sends only the fields that changed, as $set and $unset by path, so fields the agent did not touch keep their order and types. A changed array is written whole.

Does it work with a standalone MongoDB server or a sharded cluster?

No. MongoDB has transactions only on a replica set, so a standalone server is refused at attach, and sharded clusters are not supported yet.

Written by

Abhishek DG

Co-founder & CEO, 2090 Labs

Abhishek builds Chronos in Bangalore: the database where AI agents work in their own copy of your data. Before it he built TailsMart and ACRED India.

More about Abhishek

Keep reading

How to let an AI agent write to MongoDB safely · Chronos