Node.js and MongoDB - Getting started with MongoJS
来源:互联网 发布:ubuntu不支持vim 编辑:程序博客网 时间:2024/05/21 19:36
Node.js and MongoDB - Getting started with MongoJS
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 enterprises are leveraging these two technologies to deliver a better experience to their users by build more capable, performant 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 across 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 MongoJS
MongoJS 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 MongoJS
Once Node.js has been setup correctly on your machine, you can use its internal package manager NPM to install MongoJS:
$ npm install mongojs
We can now start building our JavaScript application and connect to our MongoDB server:
// app.js
var databaseUrl = "mydb"; // "username:password@example.com/mydb"
var collections = ["users", "reports"]
var db = require("mongojs").connect(databaseUrl, collections);
The databaseUrl can 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, the databaseUrl can contain just the actual database name 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.js
db.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 performing.
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 would do it in the console, your code will look like this:
// app.js
db.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.js
db.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!
转自:http://howtonode.org/node-js-and-mongodb-getting-started-with-mongojs
- Node.js and MongoDB - Getting started with MongoJS
- Node.js知识学习之——Node.js and MongoDB – Getting started with MongoJS
- Getting Started With Node.js and mongoDB
- GETTING UP AND RUNNING WITH NODE.JS, EXPRESS, JADE, AND MONGODB
- Node.js和MongoDB——从MongoJS开始
- MongoDB--Getting Started with Java Driver
- Getting Started with Eclipse and the SWT
- Getting Started With Ruby and Rails
- Getting Started with Eclipse and Counterclockwise
- Getting Started with Kinect and Processing
- Getting Started With IronRuby And RSpec
- Getting started with OpenCL and GPU Computing
- Getting started with GWT, Maven and Eclipse
- Getting Started with JPA and Eclipse
- Getting started with OpenCL and GPU Computing
- Java 开发者如何入门 Node.js 英文原文:Getting Started with Node.js for the Java Developer
- Getting Started with MongoDB (Python Edition) || 开始使用MongoDB
- MongoDB Manual 2.6 学习笔记-Getting Started with MongoDB
- C#读写文件总结
- Qt编程,从Qt Creator 开始
- 面试总结2012-6-15
- PHP REST架构简单设计
- SQLserver2012
- Node.js and MongoDB - Getting started with MongoJS
- 12年,关于今年暑假的一些计划。
- 自动分配IP地址让局域网更高效的管理--DHCP
- 黑马程序员--java高新技术--eclipse开发工具
- java环境变量配置
- 字,字节,编码,unicode,ansi
- 数据的查询、汇总 、统计和分析
- 一、【散记】linux下驱动开发--__func__介绍
- eclipse快捷键