Node. js and MongoDB-Getting started with parse js

Source: Internet
Author: User
Tags mongodb server mongodb setup

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 plain 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 $ ?dintroducing =jsmongojs 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 implements jsonce Node. js has been setup correctly on your machine, you can use its internal package manager NPM to install unzip js: $ npm install unzip jswe 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 (" Callback JS "). connect (databaseUrl, collections); The databaseUrl can contain in the database server host and port along with the database name to connect. by default the host is "localhost" and the port is "27017". if you're using the default, which is likely on a developer environment, the databaseUrl can contain just the actual database na Me for our app. the collections is 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 para Digm 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") ;}); www.2cto. comHere'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. jsAnd 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.