操作Mongodb数据库及性能测试

操作Mongodb数据库,性能测试

  • 目标:基于官方的node-mongodb-natice驱动,封装一个更小、更快、更灵活的DB模块,让我们用nodejs操作Mongodb数据库更方便、更灵活。

  • 官方文档

引入mongodb模板

cnpm install mongodb --save

开启mongodb数据库

sudo mongod
...
mongo

连接mongodb数据库

const MongoClient = require('mongodb').MongoClient;


// Connection URL
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'koa';

// Create a new MongoClient
const client = new MongoClient(url);

// Use connect method to connect to the Server
client.connect(function(err) {
  
  console.log("Connected successfully to server");

  const db = client.db(dbName);

  client.close();
});

插入一条数据

const MongoClient = require('mongodb').MongoClient;

// Connection URL
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'koa';

// Create a new MongoClient
const client = new MongoClient(url, { useUnifiedTopology: true });

// Use connect method to connect to the Server
client.connect(function(err) {
    console.log("Connected successfully to server");

    const db = client.db(dbName);

    db.collection('user').insertOne({ 'username': '阿牛111', 'age': 11, 'sex': '男', 'status': 1 }, function(err, result) {
        if (err) {
            console.log(err);
        } else {
            console.log('增加数据成功');
        }
        client.close();
    })


});

查询全部数据

const MongoClient = require('mongodb').MongoClient;

// Connection URL
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'koa';

// Create a new MongoClient
const client = new MongoClient(url, { useUnifiedTopology: true });

// Use connect method to connect to the Server
client.connect(function(err) {
    console.log("Connected successfully to server");

    const db = client.db(dbName);

    db.collection('user').find({}).toArray(function(err, docs) {
        console.log("Found the following records");
        console.log(docs)
    });


});

测试性能

const MongoClient = require('mongodb').MongoClient;

// Connection URL
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'koa';

// Create a new MongoClient
const client = new MongoClient(url, { useUnifiedTopology: true });

console.time('start');

// Use connect method to connect to the Server
client.connect(function(err) {
    console.log("Connected successfully to server");

    const db = client.db(dbName);



    db.collection('user').insertOne({ 'username': '阿牛444', 'age': 44, 'sex': '男', 'status': 1 }, function(err, result) {
        if (err) {
            console.log(err);
        } else {
            console.log('增加数据成功');
        }
        client.close();

        console.timeEnd('start');
    })

});

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ny2YZ4Ow-1599263791830)(/Users/mac/Desktop/MarkDown /nodejs笔记/koa/koa笔记三/1.jpg)]

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值