nodejs数据存mongodb:首先创建数据库保存用户信息数据库创建名 users 集合并插入条用户信息前没 users 集合mongodb 直接创建> db.users.insert( { "userId":1, "name":"tom", "email":"tom@nodejs.org" })查找信息使用 find 或者 findOne区别于 findOne 返结db.users.findOne( {"userId": 1})返结:{"_id" : ObjectId("5413be6e9e1c9f9c4386756d"),"userId" : 1,"name" : "tom","email" : "tom@nodejs.org"}驱程序编辑 package.json, 添加于 mongodb 引用{"name": "express-api","version": "0.0.1","dependencies": {"express": "2.5.9","ejs": "0.4.2","mongodb": "1.4.1"}}重新 npm install 安装 mongodb 驱使用 MongoDB 数据库修改代码首先 require mongodb 模块连接 mongodb 数据库var mongo = require("mongodb");var express = require("express");var app = express.createServer();app.set("view engine", "ejs");app.set("views", __dirname + "/views");app.set("view options", { layout: false });app.get("/", function (request, response) {response.render("index");});app.get("/user/:id", function (request, response) {var id = request.params.id;console.log(id);app.users.findOne({ "userId": +id }, function (error, doc) {if (error) return next(error);response.json(doc);});});// connect mongodb var server = new mongo.Server("127.0.0.1", 27017);var db = new mongo.Db("members", server, {safe:true }).open(function (error, client) {if (error) throw error;console.log("\033[96m + \033[39m connected to mongodb");app.users = new mongo.Collection(client, "users");client.ensureIndex("users", "userId", function (error) {if (error) throw error;console.log("\033[96m + \033[39m ensured index.");console.log("Web Server listening ......");app.listen(3000);});});注意现数据库查找用户id 前面 + 用表单字符串类型数据转换需要数字类型app.users.findOne({ "userId": +id }, function (error, doc) {if (error) return next(error);response.json(doc);});