Asrin Dayananda
Azcodez

Azcodez

Setup Mongo Database and insert noSQL data with JavaScript

Setup Mongo Database and insert noSQL data with JavaScript

Install mongo database locally and insert some data via command line and javascript

Subscribe to my newsletter and never miss my upcoming articles

In below steps we are going to install mongo database locally and insert some data via command line and also javascript script.

  • Install Mongo via this link

  • Create a new directory

    mkdir mongo
    
  • Start up mongo database into directory your using for mongo eg.

    "C:\Program Files\MongoDB\Server\5.0\bin\mongod.exe" --dbpath="<YOUR_DIRECTORY>\mongo"
    

Mongo is running now. Now we are going to add some data via command line

  • Install mongosh

  • Open command line

    mongosh
    
  • See all your current dbs by running

    show dbs
    
  • Create a new db

    use newTestDB
    
  • Create a new collection

    Insert a new document in the notes including (name, description)
    db.notes.insert({name: "Az", description: 205})
    

Now we are going to create collection and insert data via some JavaScript

  • Install node js if you havent already nodejs.org/en

  • Create a new directory for your JavaScript files

    mkdir mongo
    
  • In your project directory install

    npm install mongoose --save
    
  • Add a JS file and add below

  • This creates new collection called anotherTest and adds some data to it
// Insert into anotherTest db
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/anotherTest', {useNewUrlParser: true, useUnifiedTopology: true});

const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  // we're connected!
  console.log("connected")
});

const personSchema = new mongoose.Schema({
  name: String
});

const Kitten = mongoose.model('Person', personSchema);

const Person = new Kitten({ name: 'Az' });

Person.save(function (err, Person) {
  if (err) return console.error(err);
});
  • Run your script by running

    node <YOUR_JS_FILE>
    
  • Confirm by looking up your data in MongoDB Compass as below

image.png

References:

Hope this solution worked for you! If this didn't please comment and I will try to help you. Remember to like, comment and share Happy Coding, Asrin

If this helped you consider buying me a coffee :)

Did you find this article valuable?

Support Asrin Dayananda by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this