美文网首页我爱编程
MongoDB增删改查

MongoDB增删改查

作者: 不睡觉呀 | 来源:发表于2018-04-14 22:58 被阅读0次

    1.创建一个MongoDB模块

    var mongodb = require('mongodb');
    var server = mongodb.Server('192.168.1.114',27017,{});
    var client = new mongodb.Db('education',server,{w:true});
    
    openclient();
    function openclient(){
        client.open(function(error,client){
            if(error){
                console.error(error);
            }else{
                console.log('(1).openclient successful');
                openCollection();
            }
        });
    }
    

    创建一个MongoDB的模块,并创建一个名为“education”的系统,将其打开。

    2. 打开所创建的系统模块中的某个文档

    function openCollection(){
        client.collection('student',function(error,collection){
            if(error){
                console.error(error);
            }else{
                console.log("(2).openCollection successful");
                // insertMsg(collection);
                inquireAboutMsg(collection);
                // updateMsg(collection);
                // deleteMsg(collection);
    
            }
        });
    }
    

    打开所属于client下面的collection文档,并且查询信息inquireAboutMsg(collection);

    3.在collection文档中插入信息

    function insertMsg(collection){
        var studentX = {studentId:14404233,studentName:'xuan',studentScore:100};
        var studentH = {studentId:14404239,studentName:'HJ',studentScore:99};
        var studentF = {studentId:14404235,studentName:'huanhuan',studentScore:88};
        var student = [studentX,studentH,studentF];//插入多个对象到collection里面
        collection.insert(student,{safe:true},function(error,student){
            if(error){
                console.error(error);
            }else{
                console.log('(3).insertMsg successful');
                console.log(student);
    
    
            }
        });
    }
    

    collection.insert(student,{safe:true},function(error,student)这里的的student是插入collection中的信息。

    4.查询collection中的所有信息

    function inquireAboutMsg(collection){
        collection.find({}).toArray(function(error,student){
            if(error){
                console.error(error);
            }else{
                console.log('(4). inquireAboutMsg successful');
                console.log(student);//这里的student参数是查看当前collection里面的所有信息
            }
        });
    }
    

    find里面写空则是查询collection中的所有信息。

    5 .更新collection中的信息

    function updateMsg(collection){
        var callback = function(error,student){
            if(error){
                console.error(error);
            }else{
                console.log('(5).update successful');
                console.log(student);//这里的student参数是更新的个数
            }
        }
        collection.update({},{$set:{studentSex:'female'}},{multi:true},callback);//multi为true的时候更新collection里面的所有信息
    }
    
    

    6.删除collection中的信息

    function deleteMsg(collection){
        collection.remove({studentId:14404233},null,function(error,student){
            if(error){
                console.error(error);
            }else{
                console.log('(6).remove successful');
                console.log(student);//这里的student参数是删除的个数
            }
        });
    }
    

    romove 中的参数限定了删除哪些信息.

    相关文章

      网友评论

        本文标题:MongoDB增删改查

        本文链接:https://www.haomeiwen.com/subject/goerkftx.html