5

I'm using MongoDB Compass to visually build my collection.

In the highlighted section, I don't see a "Document" type to insert a sub-Document. Am I doing it the right way?

enter image description here

gene b.
  • 10,512
  • 21
  • 115
  • 227

6 Answers6

5

The docs state :

For the field values, you can paste valid JSON that can be parsed by JavaScript’s JSON.parse.

If the pasted JSON is an Object (i.e. a document), the keys must be quoted in double quotes and are permitted to be escaped. The first character must be left curly brace { and the last must be right curly brace }:

{
  "email": "test@example.com",
  "phone": "555-555-1212"
}

But unfortunately the process is really counterintuitive, because if you copy the JSON object from the above example, you will not be able to paste it inside the field value unless you firstly type something i.e. a space character.

But if your object is not a valid JSON, it will be pasted (!) but not parsed (?) and it will be saved as a string.

Anyway even if you manage to store your object, you will not be able to edit it easily, and you will end up copying and pasting to an external text editor in order to achive your goal.

EDIT :

You can try using this client Robo 3T which lets you edit the document structure much more easily. Funny thing, if you edit the document with Robo 3T and reload the document in Compass, it will let you do what you intended all along... So it must be a kind of Compass UI issue/bug.

ktsangop
  • 1,013
  • 2
  • 16
  • 29
  • Robo 3T is great! Definitely more feature filled than MongoDB Compass; thanks for reminding me of the name! – Janac Meena Feb 08 '18 at 14:08
  • Robot3T/Studio3T doesn't seem to work with mongo Atlas... I take the opportunity to mention how creepy and unpractical Compas is, when we know it's been developed by mongoDB itself it's really a shame. – Felipe Nov 25 '19 at 06:19
1

You can achieve what you mean, just setting otherQuestions type as Object.

This will let you to embed another document/object , It is a JSON

This is the way to work with a NoSQL BSON based database, it is not relational, so you can't set a field type as another document, just use Object and embed there the tree as you want.

Anyway I highly recommend reading Mongo's documentation

Ferus7
  • 707
  • 1
  • 10
  • 23
1

You can select the Object type while editing from the UI as below:

enter image description here

The Object can have multiple attributes of different types. When queried using an API, the data comes as below:

enter image description here

Prateek Bhuwania
  • 755
  • 1
  • 8
  • 19
1

You can simply assign a type object to the field/variable name for that sub-document

Adding array of sub-documents using MongoDB Compass

flowgician
  • 63
  • 1
  • 1
  • 7
1

MongoDB Compass seems to have recently added the JSON view feature: enter image description here

So, switch from the List view to that, then hit Edit Document on whichever document you want to change. You are now editing the document's JSON representation directly. Hit Update when you're done with your change, assuming the JSON is still valid after your changes.

This helps a lot if you need to add larger pieces of data to existing documents for example (arrays of objects, etc.) versus adding one prop at a time and selecting types from minuscule dropdowns.

Voicu
  • 16,921
  • 10
  • 60
  • 69
0

Better late than never: I think the fastest way to insert complex data into a MongoDB collection is simply to export the table, then truncate the table (delete all records), modify whatever you want in a nice text editor (notepad++, sublime, etc) and finally import the data back into the table.

Done! :)

Pedro Araujo Jorge
  • 642
  • 2
  • 9
  • 19