Give each chat a stable document ID
Use the chat’s stable identifier as the _id of a MongoDB document and update only the preference that changed. For a multilingual Telegram bot, one document can hold the language for one chat. When a user chooses another language, write to that same document. A username or chat title can change and makes a poor identifier.
Connect to your Bothosting MongoDB deployment with the client identity and server trust file from Connection. Its X.509 identity authenticates against $external and has read/write access to app. Select app for the collection that holds the preferences. See the MongoDB shell connection example.
Save the first choice and update it
Try this in a test deployment using mongosh. The example builds a string key with a platform prefix so chats from different services can use the same collection. Substitute the incoming chat ID in your bot’s handler.
const prefs = db.getSiblingDB("app").getCollection("chat_preferences")
const chat = { _id: "telegram:10001" }
prefs.updateOne(chat, { $set: { language: "en" } }, { upsert: true })
prefs.updateOne(chat, { $set: { language: "de" } }, { upsert: true })
prefs.findOne(chat, { language: 1 })The first call creates the missing document. The second changes its language to de. Using $set on that field preserves other preferences such as a time zone. MongoDB’s updateOne reference.
Check a repeated event and a second chat
Repeat the second update and run prefs.countDocuments(chat). Expect one matching document. Repeat the test with a second chat ID and a different language. Have the bot read the preference on its next message, including after a restart. If the shell returns the expected language but the bot always uses its default, compare its database name, collection and exact key with the shell example.
A separately hosted bot and database use two deployment slots. Use the MongoDB resource minimums when budgeting for both.