Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB Compass Visually Insert Sub-Document

Tags:

mongodb

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

like image 829
gene b. Avatar asked Oct 11 '17 15:10

gene b.


5 Answers

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": "[email protected]",
  "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.

like image 144
ktsangop Avatar answered Nov 18 '22 05:11

ktsangop


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

like image 1
Ferus7 Avatar answered Nov 18 '22 06:11

Ferus7


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

like image 1
Prateek Bhuwania Avatar answered Nov 18 '22 04:11

Prateek Bhuwania


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

Adding array of sub-documents using MongoDB Compass

like image 1
flowgician Avatar answered Nov 18 '22 05:11

flowgician


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.

like image 1
Voicu Avatar answered Nov 18 '22 04:11

Voicu