Skip to content Skip to sidebar Skip to footer

Where Are the Mongo Default Log Location in Windows

How to setup a local anesthetic MongoDB Connection

31st Jul 2019

I always use MongoDB as a database when I work on an app. And I like to connect to a database on my reckoner because it speeds astir dev and test-related work.

Today, I want to share how to create and connect to a local MongoDB Database.

Installing MongoDB

You want to install MongoDB on your computer before you rump tie in to it. You can install MongoDB by succeeding these instructions (Mackintosh and Windows).

Once you take in realized the initiation process, try typing mongo --version into your bidding line. You should get a reaction synonymous to the following:

          mongo --translation                  
Terminal showing MongoDB Version

Starting MongoDB

You can depart MongoDB on your computer with the mongod command.

          mongod                  

Keep off the mongod windowpane running when you want to work with your local MongoDB. MongoDB stops when you close the window.

Brief overview of how MongoDB works

MongoDB lets you store things (called documents) inside databases. Each database contains multiple collections.

To arrive easier to understand, you arse think of MongoDB as a building. IT contains many rooms.

Each room is a database. Each database is responsible for storing information about one application program. You lavatory stack away as more than selective information as you want.

You have an unlimited supply of boxes in apiece room. Each box is a aggregation. From each one collection can but carry one typecast of data.

For example, one collection can be used for books, one collection for users, one collection for toys, and and then connected.

Adding items to a database

Peerless way to add items to a MongoDB database is through the Mongo Shell. To harsh up the Mongo Shell, you open another dictation line window and campaign mongo.

          mongo                  

Note: Make sure you keep the mongod window open! You won't personify capable to interact with the Mongo Shield if you close the mongod windowpane.

First, we need a database to solve with. You can see the currently elect database with the db overlook. (Away default, you should get on the exam database).

          > db                  

Note: The > in the code preceding signifies the Mongo Shell. You don't need to type >. It is non split up of the program line.

Terminal returns 'test' when we run the 'db' command.

For this article, we'll make up a database called game-of-thrones. You john use the use up <database> command to create and switch to a new database.

          > use of goods and services plot-of-thrones                  
Switch to a database named game-of-thrones.

We're departure to hyperkinetic syndrome a character into the crippled-of-thrones. Here, we need to arrange the role into a collection. We'll use characters as the name of the collection.

To add an item to a collection, you seat pass a JavaScript object into db.<collectionName>.insertOne().

          db.characters.insertOne({ name: 'Jon Snow' })                  
Added 'Jon Snow' into the 'characters' collection.

Let's add one and only fibre into the database before we continue.

          db.characters.insertOne({ name: 'Arya Stark' })                  
Added 'Arya Stark' into the 'characters' collection.

You rear construe with the characters we've added by using the find command. (db.<collectionName>.get hold()).

          db.characters.find()                  
Database returns two characters—Jon Snow and Arya Stark.

This is whol you need to know about the Mongo Beat for now.

Accessing MongoDB with MongoDB Circumnavigate

MongoDB Compass gives you another way to access MongoDB. It's an app that makes checking (and editing) databases easier if you're non a fan of the compel credit line.

To use MongoDB Grok, you have to install it first. You can download and set u MongoDB Compass from the this page.

When you opened MongoDB Dig, you'll see a CRT screen that looks like this:

MongoDB Compass startup screen.

To unite to your localized MongoDB, you set Hostname to localhost and Larboard to 27017. These values are the default option for all topical anesthetic MongoDB connections (unless you changed them).

Sets Hostname to localhost and Port to 27017.

Entreat connect, and you should see the databases in your local MongoDB. Present, you should be able to see halting-of-thrones (the database we created for this teacher).

List of databases in my local MongoDB

If you click on crippled-of-thrones, you'll date a characters collection.

The characters collection in MongoDB Compass.

And if you click on characters, you'll get word the deuce characters we created in the earlier segment.

Documents 'Jon Snow' and 'Arya Stark' in the 'Characters' collection.

This is how you can wont MongoDB Compass to connect to a MongoDB that's running on your own computer.

Copulative to MongoDB with a Node server

When we material body applications, we colligate to MongoDB through and through our applications (not through Mongo Racing shell nor MongoDB Compass).

To connect to MongoDB, we need to economic consumption the mongodb package. As an alternative, you nates also use Mongoose.

(By the way, I prefer victimization Mongoose over the MongoDB native device driver. I'll apportion why in a future clause).

Connecting with MongoDB native driver

First you have to install and want the mongodb software packag.

          npm install mongodb --save                  
          const MongoClient = require('mongodb').MongoClient                  

You can connect to your local MongoDB with this uniform resource locator:

          const url = 'mongodb://127.0.0.1:27017'                  

With the Mongo Client, you motivation to specify the database you're using after you unite to MongoDB. Here's what it looks like:

          const dbName = 'game-of-thrones' let db  MongoClient.connect(url, { useNewUrlParser: honorable }, (err, client) => {   if (slip up) revert comfort.lumber(err)    // Storing a character to the database so you can use it later   hahnium = client.db(dbName)   comfort.log(`Connected MongoDB: ${URL}`)   console.backlog(`Database: ${dbName}`) })                  
Connected to local MongoDB with MongoDB native driver.

Connecting with Mongoose

To relate with Mongoose, you need to download and want mongoose.

          npm install mongoose --save                  
          const mongoose = require('mongoose')                  

When you use Mongoose, the association url should include the database you're connecting to:

          const url = 'mongodb://127.0.0.1:27017/brave-of-thrones'                  

You stern connect to MongoDB with the relate method:

          mongoose.associate(url, { useNewUrlParser: true })                  

Here's how you can insure whether the connection succeeds.

          const atomic number 105 = mongoose.connection db.once('overt', _ => {   comfort.log('Database connected:', url) })  dubnium.along('error', err => {   console.erroneousness('connection error:', err) })                  
Connected to local MongoDB through Mongoose.

If you enjoyed this article, please distinguish a friend about IT! Share it on Chirrup. If you spot a typo, I'd prize if you can buoy correct it on GitHub. Give thanks you!

Where Are the Mongo Default Log Location in Windows

Source: https://zellwk.com/blog/local-mongodb/

Post a Comment for "Where Are the Mongo Default Log Location in Windows"