{"library":"sharedb-mongo","title":"ShareDB MongoDB Adapter","description":"sharedb-mongo is a database adapter for ShareDB, a full-stack library for realtime JSON document collaboration. It serves as both a snapshot store and an oplog for ShareDB documents, integrating directly with MongoDB. Currently at v5.1.0, the package sees active development with regular updates to support the latest Node.js and MongoDB versions. A key differentiator is its ability to unwrap JSON document snapshots, allowing direct MongoDB queries against these documents (for read-only operations), while strictly requiring ShareDB for any modifications to ensure proper operational transformation and persistence. It also extends MongoDB's querying capabilities within the ShareDB context through special `$`-prefixed operators for methods like `mapReduce` and `sort`.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install sharedb-mongo"],"cli":null},"imports":["import ShareDbMongo from 'sharedb-mongo';","const db = require('sharedb-mongo')('mongodb://localhost:27017/test');","import ShareDbMongo from 'sharedb-mongo'; new ShareDbMongo('mongodb://localhost:27017/test', { mongoOptions: { /* ... */ } });"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import ShareDB from 'sharedb';\nimport ShareDbMongo from 'sharedb-mongo';\nimport { WebSocketServer } from 'ws';\nimport http from 'http';\n\nconst mongoUrl = 'mongodb://localhost:27017/test';\nconst db = new ShareDbMongo(mongoUrl);\nconst backend = new ShareDB({ db });\n\n// Create a WebSocket server to listen for client connections\nconst server = http.createServer();\nconst wss = new WebSocketServer({ server });\n\nwss.on('connection', (ws) => {\n  // Connect client to ShareDB backend\n  const stream = backend.connect(ws);\n  // Handle stream errors\n  stream.on('error', (err) => console.error('ShareDB stream error:', err));\n});\n\nserver.listen(8080, () => {\n  console.log('ShareDB server listening on http://localhost:8080');\n  console.log(`Connected to MongoDB at ${mongoUrl}`);\n});\n\n// Example document creation (optional, for testing)\nconst connection = backend.connect();\nconst doc = connection.get('examples', 'myDocument');\ndoc.fetch((err) => {\n  if (err) throw err;\n  if (doc.type === null) {\n    doc.create({ content: 'Hello, ShareDB!' }, 'json0', (err) => {\n      if (err) console.error('Error creating document:', err);\n      else console.log('Document created!');\n    });\n  }\n});","lang":"javascript","description":"Initializes a ShareDB backend with sharedb-mongo as the database adapter and sets up a basic WebSocket server for client connections.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}