beingkartik
beingkartik12mo ago

Migrate MongoDb documents to convex

Hey, I have two table in my mongodb which has lots of documents and now I want to migrate them to convex. I can export the documents from mongo but to import it in convex I have to remove " " from keys and its not possible to do that for large no. of documents
14 Replies
Indy
Indy12mo ago
If you're working with a large number of documents then you may want to explore https://docs.convex.dev/database/import-export/streaming Other folks have also written custom Python scripts for their particular needs
lee
lee12mo ago
To check my understanding, are you getting an error because of spaces in document keys? Spaces are generally allowed in document keys, so it would be great to see the error message. How are you attempting the import (i'm guessing npx convex import)?
beingkartik
beingkartikOP12mo ago
not spaces, "key" its string quotes are not allowed in add document. I am directly adding json data to add document
npx convex import --table test Users/kartiksharma/development.interactive-videos.csv
npx convex import --table test Users/kartiksharma/development.interactive-videos.csv
Is this the correct way to import the data?
npx convex import --table test /Users/kartiksharma/development.interactive-videos.csv
Import change summary:
table | create | delete |
-------------------------
test | 39 | 0 of 0 |
Once the import has started, it will run in the background.
Interrupting `npx convex import` will not cancel it.
✖ Importing data from "/Users/kartiksharma/development.interactive-videos.csv" to table "test" failed
Row 1 wasn't a valid Convex value: Object has too many fields (3817 > maximum number 1024)
npx convex import --table test /Users/kartiksharma/development.interactive-videos.csv
Import change summary:
table | create | delete |
-------------------------
test | 39 | 0 of 0 |
Once the import has started, it will run in the background.
Interrupting `npx convex import` will not cancel it.
✖ Importing data from "/Users/kartiksharma/development.interactive-videos.csv" to table "test" failed
Row 1 wasn't a valid Convex value: Object has too many fields (3817 > maximum number 1024)
lee
lee12mo ago
What does the data look like? Convex has limits on values so they can be retrieved efficiently. If you have objects with >1024 fields, i would restructure the data, maybe splitting it into multiple tables. https://docs.convex.dev/production/state/limits#documents
Limits | Convex Developer Hub
We’d love for you to have unlimited joy building on Convex but engineering
lee
lee12mo ago
Quotes are also allowed in document keys, although you probably don't want them. You could potentially call JSON.parse, but it would be great to see more of the code to know for sure
beingkartik
beingkartikOP12mo ago
This is my object format
lee
lee12mo ago
That doesn't look like it can be converted to csv (so i'm not sure what csv you imported). I recommend importing with jsonl format
beingkartik
beingkartikOP12mo ago
I am exporting it from mongo "visuals_by_visual_id": {}. Now its saying Row 32 wasn't a valid Convex value: Field name cannot be empty
lee
lee12mo ago
What file are you importing to get that error? Some tips: - you can make a jsonl file with https://stack.convex.dev/json-array-to-json-lines-jsonl - you will need to rename the "_id" field because it's a reserved field in convex, just like in mongo, and their formats aren't compatible (you can rename fields with jq) - you will need to rename fields starting with "$" because those are also reserved
Convert your .json array to a .jsonl (JSON Lines)
JSON Lines stores one JSON object per line in a file, which is more scalable. Here's the one-liner to convert your .json file to .jsonl
beingkartik
beingkartikOP12mo ago
All these steps I am already doing but I can't store empty value fields
"visuals_by_visual_id": {}. Now its saying Row 32 wasn't a valid Convex value: Field name cannot be empty
"visuals_by_visual_id": {}. Now its saying Row 32 wasn't a valid Convex value: Field name cannot be empty
lee
lee12mo ago
what is on row 32?
beingkartik
beingkartikOP12mo ago
I am importing this json
lee
lee12mo ago
Ah i see. Empty field names are also not allowed in convex, although i don't recall why. Can you replace occurrences of "": with something else? I'm now realizing this is what you mentioned in the original message (sorry for being slow). I'll look into why we don't support empty strings as object keys update here: we do intend to allow empty strings as object keys, as part of more generally relaxing constraints. i'll add a task internally

Did you find this page helpful?