一、准备工作
1、在mongodb创建将要读取的表
创建数据库mongotest
use mongotest;
向user表中插入数据
db.user.insert({name:'flyoung',age:'18',sex:true});
2、安装node-mongodb-native
npm install mongodb
二、实例(node.js读取mongodb)
参考node-mongodb-native的文档:https://github.com/mongodb/node-mongodb-native
var mongodb = require('mongodb');
var server = new mongodb.Server("127.0.0.1",27017,{});//本地27017端口
new mongodb.Db('mongotest',server,{}).open(function(error,client){//数据库:mongotest
if(error) throw error;
var collection = new mongodb.Collection(client,'user');//表:user
collection.find(function(error,cursor){
cursor.each(function(error,doc){
if(doc){
console.log("name:"+doc.name+" age:"+doc.age);
}
});
});
});
运行:
node mongodbTest.js
下面附一个简单的实例:
参考:http://mongodb.github.com/node-mongodb-native/api-articles/nodekoarticle1.html
var mongo = require('mongodb'),
Server = mongo.Server,
Db = mongo.Db;
var server = new Server('localhost', 27017, {auto_reconnect: true});
var db = new Db('exampleDb', server);
//db.open(function(err, db) {
// if(!err) {
// console.log("We are connected");
// }
//});
//四种连接数据库的方式
//db.open(function(err, db) {
// if(!err) {
// db.collection('test', function(err, collection) {});
//
// db.collection('test', {safe:true}, function(err, collection) {});
//
// db.createCollection('test', function(err, collection) {});
//
// db.createCollection('test', {safe:true}, function(err, collection) {});
// }
//});
//插入数据
//db.open(function(err, db) {
// if(!err) {
// db.collection('test', function(err, collection) {
// var doc1 = {'hello':'doc1'};
// var doc2 = {'hello':'doc2'};
// var lotsOfDocs = [{'hello':'doc3'}, {'hello':'doc4'}];
//
// collection.insert(doc1);
//
// collection.insert(doc2, {safe:true}, function(err, result) {});
//
// collection.insert(lotsOfDocs, {safe:true}, function(err, result) {});
// });
// }
//});
//更新数据
//db.open(function(err, db) {
// if(!err) {
// db.collection('test', function(err, collection) {
// var doc = {mykey:1, fieldtoupdate:1};
//
// collection.insert(doc, {safe:true}, function(err, result) {
// collection.update({mykey:1}, {$set:{fieldtoupdate:2}}, {safe:true}, function(err, result) {});
// });
//
// var doc2 = {mykey:2, docs:[{doc1:1}]};
//
// collection.insert(doc2, {safe:true}, function(err, result) {
// collection.update({mykey:2}, {$push:{docs:{doc2:1}}}, {safe:true}, function(err, result) {});
// });
// });
// }
//});
//删除数据
//db.open(function(err, db) {
// if(!err) {
// db.collection('test', function(err, collection) {
// var docs = [{mykey:1}, {mykey:2}, {mykey:3}];
//
// collection.insert(docs, {safe:true}, function(err, result) {
//
// collection.remove({mykey:1});
//
// collection.remove({mykey:2}, {safe:true}, function(err, result) {});
//
// collection.remove();
// });
// });
// }
//});
//查找数据
db.open(function(err, db) {
if(!err) {
db.collection('test', function(err, collection) {
var docs = [{mykey:1}, {mykey:2}, {mykey:3}];
collection.insert(docs, {safe:true}, function(err, result) {
collection.find().toArray(function(err, items) {
// console.log(items);
});
var stream = collection.find({mykey:{$ne:2}}).streamRecords();
stream.on("data", function(item) {
// console.log(item);
});
stream.on("end", function() {});
collection.findOne({mykey:1}, function(err, item) {
// console.log(item);
}) ;
});
collection.find(function(error,cursor){
cursor.each(function(error,doc){
if(doc){
console.log("mykey:"+doc.mykey);
}
});
});
});
}
});