For my testing purposes, I am attempting to transfer data from a custom JSON database to MongoDB. The data is currently stored in a json file with the following format:
{ "_id": "213124123114",
"foo":"bar",
"otherId": "2324242424",
...
}
To maintain the relationships within my test data, I aim to use sed to enclose all _id and xxxId values with ObjectId(...)
The modified data would appear as follows:
{ "_id": ObjectId("213124123114"),
"foo":"bar",
"otherId": ObjectId("2324242424"),
...
}
I intend to then insert this formatted data into MongoDB.
While testing my regular expressions in JavaScript, I encounter an issue with the following assignment:
var y = s/"_id":(\s?"[0-9]+"),/ObjectId($1)/gi
SyntaxError: Unexpected token :
Despite attempting to escape the ':', it does not resolve the problem.
If I remove the capture flag at the beginning, the regex assignment functions correctly:
var y = /"_id":(\s?"[0-9]+"),/
var p = "\"_id\": \"123123123121321212312\",";
y.test(p) === true
However, without capturing the value block, I am unable to wrap it as needed.
Any suggestions on what could be causing this issue?