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?
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.
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
You can select the Object type while editing from the UI as below:
The Object can have multiple attributes of different types. When queried using an API, the data comes as below:
You can simply assign a type object to the field/variable name for that sub-document
MongoDB Compass seems to have recently added the JSON view feature:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With