使用node操作mongodb

let mongodb = require('mongodb');
let MongodbClient = mongodb.MongoClient;
MongodbClient.connect('mongodb://127.0.0.1:27017/project', function (err, db) {
    if (err) {
        console.log(err);
    } else {
        let collection = db.createCollection('myuser');
        let mycollection = db.collection('myuser');
        mycollection.insert([{
            name: 'xiaobai',
            sex: '',
            age: 18,
            address: '北京',
            grades: 80
        }, {
            name: 'xiaobai1',
            sex: '',
            age: 12,
            address: '英国',
            grades: 94
        }, {
            name: 'xiaobai2',
            sex: '',
            age: 18,
            address: '北京',
            grades: 90
        }, {
            name: 'xiaobai3',
            sex: '',
            age: 18,
            address: '北京',
            grades: 100
        }, {
            name: 'xiaobai4',
            sex: '',
            age: 18,
            address: '北京',
            grades: 80
        }, {
            name: 'xiaobai5',
            sex: '',
            age: 20,
            address: '天津',
            grades: 95
        }], function (err, result) {
            if (!err) console.log('ok');
            // 插入多条使用insertMany([])
        })
        mycollection.find().toArray(function (err, data) {
            if (!err) console.log('ok');
        });
        mycollection.distinct('name', function (err, data) {
            if (!err) console.log('ok');
        });
        mycollection.find({
            'age': {
                $gt: 12
            }
        }, {
            name: 1
        }).toArray(function (err, data) {
            if (!err) console.log('ok');
            // 查询年龄大于12学生姓名
        });
        mycollection.find({
            'age': {
                $gt: 18,
                $lt: 25
            }
        }).toArray(function (err, data) {
            if (!err) console.log('ok');
            // 查询年龄在18--25之间的学生所有信息
            // console.log(data)
        });
        mycollection.find({
            $or: [{
                'age': {
                    $gt: 30
                }
            }, {
                sex: ''
            }]
        }).toArray(function (err, data) {
            if (!err) console.log('ok');
            // 查询女生或年龄大于30的学生信息
            // console.log(data)
        });
        mycollection.find({
            'address': '北京'
        }).toArray(function (err, data) {
            if (!err) console.log('ok');
            // 查询学生地址中包含“北京”的学生信息

        });
        mycollection.find({
            'name': /^xiaobai/
        }).toArray(function (err, data) {
            if (!err) console.log('ok');
            // 查询姓xiaobai的 学生信息
            // console.log(data)
        });
        mycollection.find().skip(2).limit(2).toArray(function (err, data) {
            if (!err) console.log('ok');
            // 查询第3--4条数据
            // console.log(data)
        });
        mycollection.find().limit(3).toArray(function (err, data) {
            if (!err) console.log('ok');
            // 查询前三条数据
            // console.log(data)
        });
        mycollection.find({}, {
            name: 1,
            grades: 1
        }).sort({
            grades: -1
        }).toArray(function (err, data) {
            if (!err) console.log('ok');
            //按照学生的成绩降序查询学生姓名和成绩
        });
        // mycollection.remove({age:{$gte:18}},function(err,result){
        //     if(!err)console.log('ok');
        //     // console.log(result)
        // })
        // mycollection.remove({},function(err,result){
        //     if(!err)console.log('ok');
        //     // console.log(result)
        // })
        mycollection.find({
            age: {
                $lte: 20
            },
            sex: ''
        }).toArray(function (err, data) {
            if (!err) console.log('ok');
            // console.log(data);
            // 查询年龄小于等于20的男生的信息
        });
        mycollection.updateMany({
            sex: ''
        }, {
            $set: {
                grades: 100
            }
        }, function (err, data) {
            if (err) console.log(err);
            // console.log(data.message.Response.data.toString());
            // console.log(data)
            // db.close(); //关闭数据库
        });
        mycollection.drop({}, function (err, message) {
            console.log(err, message)
            // 删除表
            return console.log(arguments)
        });
    }

})

猜你喜欢

转载自www.cnblogs.com/l8l8/p/9298648.html