First, we need to create a new database:
- To create a new database or switch to an existing database, you need to run: use <name of the database>. Let's create a blog database:
use blog
- Now we need to create a collection called posts, and you need to save the data directly in JSON format using the db.<your-collection-name>.save({}) command:
db.posts.save({ title: 'Post 1', slug: 'post-1', content: '<p>Content</p>' })
- As you can see, I'm not adding any id value, and that is because MongoDB automatically creates a unique ID for each row called _id, which is a random hash. If you want to see the data that you just saved, you need to use the find() method without any parameters:
db.posts.find()
- You should see your data ...