Node. js knowledge learning-node. js and MongoDB-getting started with idea JS

Source: Internet
Author: User
Tags mongodb server mongodb setup
Node. js and MongoDB-getting started with parse JS

Follow node. js and push new knowledge every day

It won't be an exaggeration if one claims that in the past few months node. js and MongoDB have literally taken the software and web industries by storm.

Not just bleeding-edge startups but even medium and large incluises are leveraging these two technologies to deliver a better experience to their users by building more capable, could T and scalable apps.

So what is node. js?

Node. JS is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. node. JS uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications
That run iis ss distributed devices.

... And what is MongoDB?

MongoDB is a scalable, high-performance, open source nosql database.

This post will cover the basics and get you started with your node. js + MongoDB app. Make sure you have node. js installed and MongoDB setup on your developer machine.

Let's verify your node. js installation and start the MongoDB Server:

$ node -v$ mongod

Introducing scripting JS
Javasjs is a brilliant little node. js package that lets you access MongoDB using an API that is extremely similar to MongoDB's JavaScript shell.

Installing upload JS
Once node. js has been setup correctly on your machine, you can use its Internal Package Manager NPM to install unzip JS:

$ npm install mongojs

We can now start building our JavaScript Application and connect to our MongoDB Server:

// app.jsvar databaseUrl = "mydb"; // "username:password@example.com/mydb"var collections = ["users", "reports"]var db = require("mongojs").connect(databaseUrl, collections);

ThedatabaseUrlCan contain the database server host and port along with the database name to connect to. By default the host is"localhost"And the port is"27017".
If you're using the default, Which is likely on a developer environment,databaseUrlCan contain just the actual database name for our app.

ThecollectionsIs a set (array) of collections our application uses. It isn' t mandatory but is preferred as it allows us to emulate a MongoDB JavaScript client like API within our node. js app.

Here's an example for finding documents within a collection specifically in this case to find all female users.

// app.jsdb.users.find({sex: "female"}, function(err, users) {  if( err || !users) console.log("No female users found");  else users.forEach( function(femaleUser) {    console.log(femaleUser);  } );});

Notice how our initial query is a near duplication of the corresponding query in MongoDB's console. in addition to the query, in our node. JS source code (I. e. app. JS) we pass a callback function to handle the results of the query.

Node. js implements an event based concurrency paradigm and almost everything is always a callback. This allows your node. js app to be non-blocking and high discovery Ming.

What happens in our specific callback is self-explanatory-we check for errors and results, and if the query returns female users they are logged to the console.

Okay, how do I save a new user in my collection? Exactly how you wowould do it in the console, your code will look like this:

// app.jsdb.users.save({email: "srirangan@gmail.com", password: "iLoveMongo", sex: "male"}, function(err, saved) {  if( err || !saved ) console.log("User not saved");  else console.log("User saved");});

Here's an example for updating a record/document:

// app.jsdb.users.update({email: "srirangan@gmail.com"}, {$set: {password: "iReallyLoveMongo"}}, function(err, updated) {  if( err || !updated ) console.log("User not updated");  else console.log("User updated");});

Okay, now we run this app in the console:

$ node app.js

And there we have it, an incredibly simple Quick Start for node. js + MongoDB enthusiasts. Happy coding!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.