Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB Compass can't insert document by pasting json string

I'm using compass community v1.13.1 and have been trying to populate some sample data for couple of hours, but failed.

Found a similar issue here, I followed the first answer by @ktsangop but it didn't work.

Cant paste as a json object anyway, if I set type to string, it will be just blank string; if set type to object, can't paste the json string as a whole. image

Do I need to insert a valid json object one field by one field? or am I doing it wrong?

like image 599
Tyler Xue Avatar asked May 18 '18 02:05

Tyler Xue


People also ask

Why my MongoDB compass is not working?

Ensure Your MongoDB Instance is Running Compass must connect to a running MongoDB instance. Make sure you have installed MongoDB and have a running mongod process. You should also check that the port where your MongoDB instance is running matches the port you provide in the Compass connect dialog.

Does MongoDB accept JSON?

Does MongoDB use BSON or JSON? MongoDB stores data in BSON format both internally, and over the network, but that doesn't mean you can't think of MongoDB as a JSON database. Anything you can represent in JSON can be natively stored in MongoDB, and retrieved just as easily in JSON.


2 Answers

For future visitors: this answer might help.

Just switch the view to JSON (the two curlies button - a feature that was added after this question was raised it seems like), hit "Edit Document" upon hovering a document, and then you will be editing the JSON directly which gives you a lot of flexibility and speed.

like image 125
Voicu Avatar answered Oct 12 '22 11:10

Voicu


Can't believe that this is an actual issue.. for the first 2 minutes I though that something is wrong with me lol

Anyhow, I do have a (hacky) solution:

  1. Select an item and click the Edit Document button
  2. Create a new field or select existing one for editing
  3. Enter a double-quote sign "
  4. Copy the json payload
  5. Seek to the beginning of the field and remove the double-quote sign (in step 3)
  6. Save document changes

TADA!


UPDATE:

Seems like the copy-paste action will only take effect if the json is partial (valid jsons are ignored for some reason).. so I ended-up with an easier solution:

  1. Select an item and click the Edit Document button
  2. Create a new field or select existing one for editing
  3. Omit the last character from your json payload (for { "a": true } copy only { "a": true)
  4. Copy the partial json payload
  5. Seek to the end of the field add the missing character (in our sample })
  6. Save document changes
like image 40
ymz Avatar answered Oct 12 '22 11:10

ymz