{"id":44156,"library":"y-mongodb","title":"y-mongodb","description":"A MongoDB database adapter for Yjs, providing persistent storage for collaborative editing applications using y-websocket. This package is a fork of Y-leveldb and is not officially maintained by the Yjs team. Current stable version is 0.1.11, with infrequent releases. It requires MongoDB as a backend and offers a simple API for storing and retrieving Yjs documents. The package ships TypeScript types and is designed to be used in Node.js environments.","status":"active","version":"0.1.11","language":"javascript","source_language":"en","source_url":"https://github.com/fadiquader/y-mongodb","tags":["javascript","Yjs","Y-mongodb","shared editing","collaboration","typescript"],"install":[{"cmd":"npm install y-mongodb","lang":"bash","label":"npm"},{"cmd":"yarn add y-mongodb","lang":"bash","label":"yarn"},{"cmd":"pnpm add y-mongodb","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency; y-mongodb is a persistence adapter for Yjs documents.","package":"yjs","optional":false}],"imports":[{"note":"The package is primarily used with CommonJS in the examples, but TypeScript types support ESM imports.","wrong":"const MongodbPersistence = require('y-mongodb')","symbol":"MongodbPersistence","correct":"import { MongodbPersistence } from 'y-mongodb'"},{"note":"CommonJS destructured import is the documented pattern.","wrong":"const MongodbPersistence = require('y-mongodb').MongodbPersistence","symbol":"MongodbPersistence","correct":"const { MongodbPersistence } = require('y-mongodb')"},{"note":"No default export exists; only named export MongodbPersistence.","wrong":"import { default as yMongodb } from 'y-mongodb'","symbol":"default","correct":"import yMongodb from 'y-mongodb'"}],"quickstart":{"code":"import http from 'http';\nimport WebSocket from 'ws';\nimport * as Y from 'yjs';\nimport { MongodbPersistence } from 'y-mongodb';\nimport * as utils from 'y-websocket/bin/utils.js';\n\nconst mongoUri = process.env.MONGODB_URI ?? 'mongodb://localhost:27017';\nconst collection = 'yjs-transactions';\nconst persistence = new MongodbPersistence(mongoUri, collection);\n\nconst server = http.createServer((req, res) => {\n  res.writeHead(200, { 'Content-Type': 'text/plain' });\n  res.end('okay');\n});\n\nconst wss = new WebSocket.Server({ noServer: true });\n\nwss.on('connection', utils.setupWSConnection);\n\nserver.on('upgrade', (request, socket, head) => {\n  wss.handleUpgrade(request, socket, head, (ws) => {\n    wss.emit('connection', ws, request);\n  });\n});\n\nutils.setPersistence({\n  bindState: async (docName, ydoc) => {\n    if (!ydoc) return;\n    const persistedYdoc = await persistence.getYDoc(docName);\n    const newUpdates = Y.encodeStateAsUpdate(ydoc);\n    await persistence.storeUpdate(docName, newUpdates);\n    Y.applyUpdate(ydoc, Y.encodeStateAsUpdate(persistedYdoc));\n    ydoc.on('update', update => {\n      persistence.storeUpdate(docName, update);\n    });\n  },\n  writeState: async (docName, ydoc) => {\n    await persistence.storeUpdate(docName, Y.encodeStateAsUpdate(ydoc));\n    return new Promise(resolve => resolve());\n  }\n});\n\nconst port = process.env.PORT ?? 8080;\nserver.listen(port);\nconsole.log(`Listening on port ${port}`);","lang":"typescript","description":"Shows setting up a y-websocket server with MongoDB persistence using y-mongodb."},"warnings":[{"fix":"Set process.env.MONGODB_URI or pass uri as first argument to new MongodbPersistence(uri, collection).","message":"MongoDB connection string must be provided via environment variable MONGODB_URI or passed directly.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Consider using official Yjs persistence adapters like y-leveldb or y-websql for production use.","message":"The package is a fork of Y-leveldb and not officially maintained; expect limited support and infrequent updates.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Await persistence.storeUpdate in writeState before resolving.","message":"writeState in persistence callback must resolve after document is fully saved; the example shows immediate resolve which may cause data loss.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Enable esModuleInterop or use * as syntax for default exports.","message":"The package uses CommonJS require in examples; TypeScript imports may not work without esModuleInterop.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"search_vec":"'0.1.11':43 'adapt':7 'api':57 'applic':16 'backend':52 'collabor':14,85 'current':39 'databas':6 'design':71 'document':63 'edit':15,84 'environ':77 'fork':25 'infrequ':45 'javascript':78 'leveldb':29 'maintain':34 'mongodb':3,5,49,82 'node.js':76 'offer':54 'offici':33 'packag':22,65 'persist':11 'provid':10 'releas':46 'requir':48 'retriev':61 'share':83 'ship':66 'simpl':56 'stabl':40 'storag':12 'store':59 'team':38 'type':68 'typescript':67,86 'use':17,74 'version':41 'websocket':20 'y':2,19,28,81 'y-leveldb':27 'y-mongodb':1,80 'y-websocket':18 'yjs':9,37,62,79","created_at":"2026-06-05T17:03:14.877595+00:00","updated_at":"2026-06-05T17:03:14.877595+00:00","problems":[{"fix":"Set MONGODB_URI environment variable or pass a valid MongoDB connection string to new MongodbPersistence(uri, collection).","cause":"Missing MONGODB_URI environment variable or undefined passed to constructor.","error":"MongooseError: The `uri` parameter to `openUri()` must be a string, got \"undefined\". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string."},{"fix":"Check that document exists before applying update. Use Y.utils.applyUpdate only on valid Y.Doc objects.","cause":"getYDoc returned undefined or null because document not found in database.","error":"TypeError: persistedYdoc is not a Y.Doc"},{"fix":"Ensure import { MongodbPersistence } from 'y-mongodb' or const { MongodbPersistence } = require('y-mongodb').","cause":"Forgetting to import or require the class correctly.","error":"ReferenceError: MongodbPersistence is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/fadiquader/y-mongodb#readme","github":"https://github.com/fadiquader/y-mongodb","docs":null,"changelog":null,"pypi":null,"npm":"y-mongodb","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-05","next_check":"2026-09-03","install_tag":null}}