How to save y-doc in mongdoDB.

We are trying to receive a y-doc from the client and store it in MongoDB. After confirming that typeof(doc) was an object, we tried the following three methods.

  1. Convert to string and store: JSON.stringify(doc)
  2. doc.JSON()
  3. Save doc in schema as it is.

we need your help because we failed everything.

How can y-doc be stored in a commercial DB (mongoDB) rather than y-leveldb, y-indexdb?
we are university student

Hi @jinh0303,

You can encode the document to a binary object and transform it back to a Yjs document. This process is documented here: Document Updates - Yjs Docs

// encode the document to a binary object: 
storeInDatabase(Y.encodeStateAsUpdate(ydoc))

loadFromDatabase().then(state => {
  const ydoc = new Y.Doc()
  Y.applyUpdate(ydoc)
  ydoc // should now have the same information as the initial document
})

1 Like

thank you sir!!

You saved us!