MongoDB Hello World! Example


MongoDB Hello World Example

In this tutorial, let us take a quick look at how to get started with MongoDB with a Hello World! example,

  • Make sure MongoDB has been installed on your system, if not follow the article: Installing MongoDB
  • Start the mongod server,
    # modgod
    MongoDB starting : pid=5249 port=27017 dbpath=/data/db 64-bit host=b2391b608026
    db version v3.6.8
    waiting for connections on port 27017
  • Now start the MongoDB shell using mongo command,
    # mongo
    MongoDB shell version v3.6.8
    connecting to: mongodb://127.0.0.1:27017
    Implicit session: session { "id" : UUID("bc22e919-c624-40bc-9f99-5ce94de57e2c") }
    MongoDB server version: 3.6.8
  • Now lets create our first MongoDB Database using command use your-new-db-name,
    # use myMongoDb
    switched to db myMongoDb
  • Lets create our first MongoDB document called: messages, and add the "Hello World!" message,
    db.myMongoDb.messages.insert({id:1,"message":"Hello World!"});
    WriteResult({ "nInserted" : 1 })
    
  • Lets print our message, by find method,
    > db.myMongoDb.messages.find({id:1});
    { "_id" : ObjectId("6250a4d85a0cb8937f46272f"), "id" : 1, "message" : "Hello World!" }
    > 

Congratulations! You have completed the MongoDB "Hello World!" example!

Advertisements
Try Out Code2care Dev Tools:

Advertisements



#Android #Java #Linux #Microsoft #Google #Python #MacOS #Notepad++ #Microsoft Teams #CSS #PHP #SharePoint #Html #Linux #C-Program #Bootstrap #jQuery #Sublime #Android-Studio #Facebook #Eclipse #WhatsApp #News #MySQL #Json #HowTos #JavaScript #FTP #S3


Advertisements


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR - https://donate.unhcr.org/in/en-in/ukraine-emergency