How to do it...

First, we need to create a new database:

  1. 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
  1. 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>' })
  1. 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()
  1. You should see your data ...

Get React Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.