博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nodejs操作mongodb
阅读量:5058 次
发布时间:2019-06-12

本文共 3101 字,大约阅读时间需要 10 分钟。

首先安装nodejs mongodb

npm  install mongodb
 
 
 
var  mongodb = require('mongodb'),
     http = require('http'),
     url = require('url');
var  server  = 
new mongodb.Server('localhost', 27017, {auto_reconnect:
true});
var  db = 
new mongodb.Db('mydb', server, {safe:
true});
//
插入表数据
function insert(tablename,rowInfo,callback){
    db.open(
function(err, db){
        
if(!err){
            
//
db.createCollection
            db.createCollection(tablename,{safe:
true}, 
function(err, collection){
                
if(!err){
                    collection.insert(rowInfo,{safe:
true},
function(err, result){
                        
if(!err){
                            callback(
true);
                        }
else{
                            callback(
false);
                        }
                    }); 
                }
else{
                    console.log(err);
                }
            });
        }
else{
            console.log('connect db'+err);
        }
    });
}
function save(tablename,rowInfo,callback){
    db.open(
function(err,db){
        
if(err)
return console.error(err);
        
        console.log('* mongodb connected');
        db.collection(tablename).save(rowInfo,
function(err,result){
            callback(result);
            db.close();
        });
    })
}
//
修改表数据
function update(tablename,id,rowInfo,callback){
    db.open(
function(err, db){
        
if(!err){
            db.createCollection(tablename,{safe:
true}, 
function(err, collection){
                
if(!err){
                    collection.update(id,rowInfo,{safe:
true},
function(err, result){
                        
if(!err){
                            callback(
true);
                        }
else{
                            callback(
false);
                        }
                    }); 
                }
else{
                    console.log(err);
                }
            });
        }
else{
            console.log('connect db'+err);
        }
    });
}
//
删除表数据
function del(tablename,id,callback){
    db.open(
function(err, db){
        
if(!err){
            db.collection(tablename,{safe:
true}, 
function(err, collection){
                
if(!err){
                    collection.
delete(id,{safe:
true},
function(err, result){
                        
if(!err){
                            callback(
true);
                        }
else{
                            callback(
false);
                        }
                    }); 
                }
else{
                    console.log(err);
                }
            });
        }
else{
            console.log('connect db'+err);
        }
    });
}
//
查询表数据
function  find(tablename,callback){
    db.open(
function(err, db){
        
if(!err){
            db.collection(tablename,{safe:
true}, 
function(err, collection){
                
if(!err){
                    collection.find().toArray(
function(err, result){
                        
if(!err){
                            callback(result);
                        }
else{
                            console.log(err);
                        }
                    }); 
                }
else{
                    console.log(err);
                }
            });
        }
else{
            console.log('connect db'+err);
        }
    });
}
//
删除Collection
function dropCollection(tablename,callback){
    db.open(
function(err, db){
        
if(!err){
            db.dropCollection(tablename,{safe:
true},
function(err,result){
                
if(!err){
                    callback(
true);
                }
else{
                    callback(
false);
                }
            }); 
        }
else{
            console.log('connect db'+err);
        }
    });
}
var myres = "";
http.createServer(
function(req,res){
    
var arg = url.parse(req.url,
true).query;
    
var pathname = url.parse(req.url).pathname;
    console.log(pathname);
    
switch(pathname){
        
case '/find':        
            find("tb_user",
function(data){
                myres = data;
                console.log(data);
            });
        
break;
        
case '/insert':
            
var rowInfo = {'userID':10008,'userName':'ciwei','password':'123456','age':25};
            insert("tb_user",rowInfo,
function(data){
                myres = data;
            });
        
break;
        
case '/update':
            
var id = {"userID":10008};
            
var rowInfo = {$set:{"userName":"ciwei88888","password":"123456","age":25}};
            update("tb_user",id,rowInfo,
function(data){
                myres = data;
            });
        
break;
    }
    
    
var str = arg.callback + '(' + JSON.stringify(myres) + ')'
    console.log(str);
    res.end(str);
}).listen(8080,'localhost');
console.log("run on 8080");
 
 
更多资料请参见nodejs mongodb官网  和mongodb官网

转载于:https://www.cnblogs.com/wuxiang/p/4626810.html

你可能感兴趣的文章
unity3d根据手机串号和二维码做正版验证
查看>>
二十六、Android WebView缓存
查看>>
django Models 常用的字段和参数
查看>>
linux -- 嵌入式linux下wifi无线网卡驱动
查看>>
SVN使用教程总结
查看>>
SQL中varchar和nvarchar有什么区别?
查看>>
OpenCV矩阵运算总结
查看>>
Java Build Practice 4:Extend and Invoke Ant API
查看>>
[转] Transformer图解
查看>>
FreeBSD方式安装 MAC OSX
查看>>
Linux 根文件系统制作
查看>>
IOS--沙盒机制
查看>>
My.Ioc 的性能
查看>>
使用 JointCode.Shuttle 访问任意 AppDomain 的服务
查看>>
hdoj 1846 Brave Game(巴什博弈)
查看>>
Round #345 B. Beautiful Paintings(Div.2)
查看>>
51nod 1018排序
查看>>
sqlite的坑
查看>>
digitalocean --- How To Install Apache Tomcat 8 on Ubuntu 16.04
查看>>
linux swoole
查看>>