question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support Extended JSON format

See original GitHub issue

Hi @pkosiec,

Awesome job at the library, it’s very useful.

I’m running into an issue. I’m inserting data from a JSON file. In the file I have an “_id” field which should be MongoDB ObjectId.

The issue is that it gets inserted as plain text.

This syntax should lead to the creation of ObjectIds:

{ 
 "_id" : {"$oid":"5a68fdc3615eda645bc6bdec"}
}

But it throws an error locally: { MongoSeedingError: Error: key $oid must not start with '$'

I found that syntax here: https://stackoverflow.com/questions/51439955/mongoimport-id-is-not-an-objectid

Is there a way to end up with ObjectIds instead of plain text ids?

Thank you

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:15 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
pkosieccommented, Oct 28, 2020

Hi @Fiyin-Anne, Because EJSON parsing is enabled by default for all JSON files, it should work normally. Just to double check: do you have something like this in your json file?

{ 
 "_id" : {"$oid":"5a68fdc3615eda645bc6bdec"}
}

If so, it should work.

The error you provided shouldn’t be related to the EJSON parsing mode. Anyway, currently there is no support for changing mode of EJSON parsing, however I will introduce it very shortly.

1reaction
pkosieccommented, Oct 19, 2020

Hi @mtapdilli, I’m using EJSON parser from official MongoDB bson package. What I do is

return EJSON.parse(content, {
   relaxed: true,
});

https://github.com/pkosiec/mongo-seeding/blob/master/core/src/populator/filesystem.ts#L125

However, maybe the relaxed mode messes things up?

Relaxed Mode A string format that emphasizes readability and interoperability at the expense of type preservation. That is, conversion from relaxed format to BSON can lose type information.

Could you please try using canonical mode e.g. with modified mongo-seeding that uses this mode while parsing EJSON? If that will be the case, I can add additional parameters to configure EJSON parsing mode.

Cheers!

Read more comments on GitHub >

github_iconTop Results From Across the Web

MongoDB Extended JSON (v2)
Use Extended JSON v2.0 (Canonical mode) format for the metadata. Requires mongorestore version 4.2 or later that supports Extended JSON v2.0 ...
Read more >
specifications/extended-json.rst at master - GitHub
MongoDB Extended JSON is a string format for representing BSON documents. This specification defines the canonical format for representing each BSON type in...
Read more >
Extended JSON - GitHub Pages
Relaxed Mode: Relaxed representation that loses type information for BSON numeric types and uses a more human-readable representation of BSON dates. See the ......
Read more >
MongoDB Extended JSON (v2)
MongoDB Extended JSON (v2) ... . Any JSON parser can parse these strict mode representations as key/value pairs; however, only the MongoDB internal...
Read more >
MongoDB Extended JSON
JSON can only represent a subset of the types supported by BSON. To preserve type information, MongoDB adds the following extensions to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found