To insert a single document using MongoDB Compass: Navigate to the collection you wish to insert the document into: In the left-hand MongoDB Compass navigation pane, click the database to which your target collection belongs. From the database view, click the target collection name. Click the Insert Document button. Visual Studio Code on macOS Installation. Download Visual Studio Code for macOS. Open the browser's download list and locate the downloaded archive. Select the 'magnifying glass' icon to open the archive in Finder. Drag Visual Studio Code.app to the Applications folder, making it available in the macOS Launchpad.
- Insert Data into MongoDB
Author: MongoDB Documentation Team
In this guide, you will insert data into MongoDB.
Time required: 15 minutes
What You’ll Need¶
- If you have not already installed a client (e.g. a driver, MongoDB Compass, or the
mongoshell), complete the Connect to MongoDB guide before attempting this guide.
- If you have not already installed a client (e.g. a driver, MongoDB Compass, or the
mongoshell), complete the Connect to MongoDB guide before attempting this guide. - Enable Auth on your local instance of MongoDB.
Warning
If you are running MongoDB locally and have not enabled authentication, your MongoDB instance is not secure.
Check Your Environment¶
You will need to ensure that your MongoDB instance is running and accessible.
Check that you have an Atlas account and have deployed a MongoDB databasecluster. See Get Started with Atlas for informationon how to login and create a cluster in Atlas.
- Windows
- macOS
- Linux
To make sure that your MongoDB instance is running on Windows,run the following command from the Windows command prompt:
If a mongod.exe instance is running, you willsee something like:
To make sure your MongoDB instance is running on Mac, run thefollowing command from your terminal:
If a mongod instance is running, you will seesomething like:
To make sure your MongoDB instance is running on Linux, run thefollowing command from your terminal:
If a mongod instance is running, you will seesomething like:
Procedure¶
Connect to Your MongoDB Instance¶
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
- C#
- Go
Select the operating system platform on which you are running theMongoDB client you have selected.
- Windows
- macOS
- Linux
Pass the URI to the mongo shell followed by the --passwordoption. You will then be prompted for your password.
Pass the URI to the mongo shell followed by the --passwordoption. You will then be prompted for your password.
Pass the URI to the mongo shell followed by the --passwordoption. You will then be prompted for your password.
If you wish to manually configure your Compass connection, loadCompass and select the NewConnection link. You will see aform where you can enter connection information for MongoDB.
Atlas users can copy a URI string from the Atlasconsole into Compass. MongoDB Compass can detect whether you have a MongoDBURI connection string in your system clipboard and auto-populate the connection dialog from the URI.
See Set Up Atlas Connectivity for information onhow to get the Atlas connection string URI into your copybuffer.
If Compass was already running when you copied the URI string,click the NEW CONNECTION button.
You will be prompted to populate the connection dialog.Click Yes.
You should then populate the password field with theproper password for your MongoDB user in the connection form.
Note
Errors related to connecting through Compass willappear in red at the top of the Connect screen.
It’s a good idea to put your connection code in a class sothat it can be reused.
If your connection_string starts with mongodb+srv, you need to install the dnspython module with
Now add code to call the class you just created.
For the MongoDB java driver 3.7 and beyond, use the MongoClients.create() method.
For legacy drivers (prior to 3.7), use:
The MongoDB.Bson package is used in CRUD operations, soyou’ll import it here.
Replace your password and any parameters surrounded by $[]in the connection string in the code below.
For now, you will use the context.TODO().
Later you’ll configure the context specific to your requirements.
You won’t know if the connection has been successful until youuse the connection. A ping is one way you can test theconnection. This is a full example of a Go connection tomongoDB, including a test ping.
In your Go workspace and project folder, run build.
Now run the binary. For binaries that are not installed, you’llhave to specify the path.
If you’d like to run the resulting binary without specifyinga path, install the binary you just built into your Go workspace.
Now run the code. “yourprojectname” is the name of the projectdirectory that contains the file with your main() function.

For installed binaries use:
For binaries that are not installed, you’ll have to specifythe path.
The default timeout for the Go driver to connect to the databaseis 30 seconds. In the event that you are unable to connect,you will see an error that resembles this:
Switch to the test Database¶
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
- Motor
- C#
- Go
To switch to the test database in the mongo shell, type
If the database has not been created already, click theCreate Database button.
Select the test database on the left side of the Compassinterface. Compass will list all of the collections in thedatabase below the database name.
Switch to the test database andaccess the inventory collection.
Within the connect block, set db to the test database.
Switch to the test database andaccess the inventory collection.
Insert a Single Document¶
Now you are ready to create your first document in MongoDB.
MongoDB stores documents as BSON, a binary form of JavaScript ObjectNotation JSON. The documents are stored in collections.
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
- Motor
- C#
- Go
The following example inserts a new document into theinventory collection. If the document does not specifyan _id field, MongoDB adds the _id field with anObjectId value to the new document.
To insert a single document using MongoDB Compass:
Navigate to the collection you wish to insert the documentinto:
- In the left-hand MongoDB Compass navigation pane, clickthe database to which your target collection belongs.
- From the database view, click the target collection name.
Click the Insert Document button:
For each field in the document, select the field type andfill in the field name and value. Add fields by clickingthe last line number, then clickingAdd Field After …
- For
Objecttypes, add nested fields by clicking thelast field’s number and selectingAdd Field After … - For
Arraytypes, add additional elements to the arrayby clicking the last element’s line number and selectingAdd Array Element After …
- For
Once all fields have been filled out, click Insert.
The following example inserts a new document into thetest.inventory collection:
The following example inserts a new document into theinventory collection. If the document does not specifyan _id field, the PyMongo driver adds the _id fieldwith an ObjectId value to the new document. SeeInsert Behavior.
The following example inserts a new document into theinventory collection. If the document does not specifyan _id field, the driver adds the _id field with anObjectId value to the new document. SeeInsert Behavior.
First, get the inventory collection.
The following example inserts a new document into theinventory collection. If the document does not specifyan _id field, the Node.js driver adds the _id fieldwith an ObjectId value to the new document. SeeInsert Behavior.
The following example shows the api call required to insert anew document into the inventory collection. If the documentdoes not specify an _id field, the Motor driver adds the_id field with an ObjectId value to the new document. SeeInsert Behavior.
The following example inserts a new document into theinventory collection. If the document does not specifyan _id field, the C# driver adds the _id fieldwith an ObjectId value to the new document. SeeInsert Behavior.
The following example shows the api call required to insert anew document into the inventory collection. If the documentdoes not specify an _id field, the Go driver adds the_id field with an ObjectId value to the new document. SeeInsert Behavior.
Mongodb Mac Os Client
Before inserting the data, you’ll need to assign theinventory collection in the test database to avariable
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
- Motor
- C#
- Go
Next, populate a Document with fields and an embedded document,and insert it into the database.
For completeness, this is how you might wrap this call and runit with the asyncio event loop.
Run the loop:
Unlike SQL tables, MongoDB collections have dynamic schemas. That is,a single collection can store documents that differ in shapes (i.e.contain different fields and value types). And unlike SQL, no DDLoperation is required to add or remove fields or modify field types.You just update the documents directly.
Note
While MongoDB does support dynamic collection definitions, you canalso enforce a uniform schema for the documents in your collection.See JSON Schema validation.
- Python
- Java (Sync)
- Motor
When you are done working with your MongoDB data, close yourconnection to MongoDB:
Summary¶
If you have successfully completed this guide, you have created your first MongoDB data.In the next guide, you will check your work by retrieving the information you just saved.
What’s Next¶
In this guide, you will read all documents from a collection in the MongoDB database.
Mongodb Macos Big Sur
See Also¶
For other CRUD guides:
© MongoDB, Inc 2008-present. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc.
Installation
- Download Visual Studio Code for macOS.
- Open the browser's download list and locate the downloaded archive.
- Select the 'magnifying glass' icon to open the archive in Finder.
- Drag
Visual Studio Code.appto theApplicationsfolder, making it available in the macOS Launchpad. - Add VS Code to your Dock by right-clicking on the icon to bring up the context menu and choosing Options, Keep in Dock.
Launching from the command line
You can also run VS Code from the terminal by typing 'code' after adding it to the path:
- Launch VS Code.
- Open the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) and type 'shell command' to find the Shell Command: Install 'code' command in PATH command.
- Restart the terminal for the new
$PATHvalue to take effect. You'll be able to type 'code .' in any folder to start editing files in that folder.
Note: If you still have the old code alias in your .bash_profile (or equivalent) from an early VS Code version, remove it and replace it by executing the Shell Command: Install 'code' command in PATH command.
Alternative manual instructions
Instead of running the command above, you can manually add VS Code to your path, to do so run the following commands:
Start a new terminal to pick up your .bash_profile changes.
Note: The leading slash is required to prevent $PATH from expanding during the concatenation. Remove the leading slash if you want to run the export command directly in a terminal.
Note: Since zsh became the default shell in macOS Catalina, run the following commands to add VS Code to your path:
Touch Bar support
Out of the box VS Code adds actions to navigate in editor history as well as the full Debug tool bar to control the debugger on your Touch Bar:
Mojave privacy protections
After upgrading to macOS Mojave version, you may see dialogs saying 'Visual Studio Code would like to access your {calendar/contacts/photos}.' This is due to the new privacy protections in Mojave and is not specific to VS Code. The same dialogs may be displayed when running other applications as well. The dialog is shown once for each type of personal data and it is fine to choose Don't Allow since VS Code does not need access to those folders. You can read a more detailed explanation in this blog post.
Mongodb Macos Client
Updates
VS Code ships monthly releases and supports auto-update when a new release is available. If you're prompted by VS Code, accept the newest update and it will get installed (you won't need to do anything else to get the latest bits).
Note: You can disable auto-update if you prefer to update VS Code on your own schedule.
Preferences menu
You can configure VS Code through settings, color themes, and custom keybindings available through the Code > Preferences menu group.
You may see mention of File > Preferences in documentation, which is the Preferences menu group location on Windows and Linux. On a macOS, the Preferences menu group is under Code, not File.
Next steps
Once you have installed VS Code, these topics will help you learn more about VS Code:
- Additional Components - Learn how to install Git, Node.js, TypeScript, and tools like Yeoman.
- User Interface - A quick orientation around VS Code.
- User/Workspace Settings - Learn how to configure VS Code to your preferences settings.
Mongodb Macos Start
Common questions
Why do I see 'Visual Studio Code would like access to your calendar.'
If you are running macOS Mojave version, you may see dialogs saying 'Visual Studio Code would like to access your {calendar/contacts/photos}.' This is due to the new privacy protections in Mojave discussed above. It is fine to choose Don't Allow since VS Code does not need access to those folders.
VS Code fails to update
If VS Code doesn't update once it restarts, it might be set under quarantine by macOS. Follow the steps in this issue for resolution.
Mongodb Macos Installation
Does VS Code run on Mac M1 machines?
Start Mongodb Mac Os X
Yes, VS Code supports macOS ARM64 builds that can run on Macs with the Apple M1 chip. You can install the Universal build, which includes both Intel and Apple Silicon builds, or one of the platform specific builds.
