Import collection from JSON and convert _Id from String to ObjectId

Magnus Berggren shared this question 5 years ago
Answered

Hi, I'm trying to import a collection from a JSON text file.

The problem is that the _id does not have a specific type in the JSON, and gets imported as a normal string. However, I need it to be an ObjectId. Is there a way to configure the import to automatically assign a desired type, or is there a query I could run afterwards to make the conversion?

Replies (1)

photo
1

NoSQLBooster provides generated import script that you can modify and customize it to suit your needs.

1. Click "Preview" button in the import dialog.

2. Press "Alt+Shift+0" or select "Fold->Unfold all" in the context menu to unfold all editor script.

3. Go to line 37

    while (continueRead) {
        let docs = srcDocs.slice(offset, offset + BATCH_SIZE);
        let readLength = docs.length;

        //wirte your code here to transform imported doc
       //The following example sets the customer _ id field to the _ id of the imported document
        docs=docs.map(it=>{
            return {...it, _id: parseInt(it.customer_id)}
        })

Leave a Comment
 
Attach a file