MongoDB使用mongodb driver api连接mongo并增删改查(mongodb module)
Posted _less is more
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB使用mongodb driver api连接mongo并增删改查(mongodb module)相关的知识,希望对你有一定的参考价值。
1、下载4.0的driver
npm install mongodb@4.0
2、兼容性表,来源
不完全截图
3、下载mongodb 5镜像
docker run --rm -d -p 27017:27017 mongo:5
进入容器
docker exec -it 26d83e6bb443 /bin/bash
4、如果需要用config文件
可以用config module,下载
npm install config
在js里引用,其中process.env.NODE_ENV是设置config/下配置文件的名称
process.env.NODE_ENV = 'mongo';
const config = require('config');
const port = config.get('port');
const host = config.get('host');
const db = config.get('db');
const opts = config.get('opts');
console.log(port, host, db, opts);
如果不想下载config module,也可以直接读取config
const config = require('./config/mongo.json');
const port = config.port;
const host = config.host;
const db = config.db;
const opts = config.opts;
console.log(port, host, db, opts);
最后两种方法输出效果一样
4、demo应用,参考
json file
"host":"localhost","port":"27017","db":"ee547_hw","opts":"useUnifiedTopology":true
js文件
'use strict';
const MongoClient = require('mongodb');
function readConfig()
try
const config = require('./config/mongo.json');
return config;
catch (err)
// console.log(err);
process.exit(2);
const config = readConfig();
const port = config.port;
const host = config.host;
const url = `mongodb://$host:$port`;
const dbName = config.db;
const opts = config.opts;
// Connect using MongoClient
const mongoClient = new MongoClient(url, opts);
// CRUD
mongoClient.connect(async function(err, client)
if (err) process.exit(5);
// client 连接数据库管理系统成功后,连接到dbName数据库
const db = client.db(dbName);
// 删除已存在的collection,不存在也不影响
await db.dropCollection("xx").then(
(value) => console.log(value);,
(error) => console.log("collection xx may not exist.");
);
await db.dropCollection("player").then(
(value) => console.log(value);,
(error) => console.log("collection player may not exist.");
);
await db.dropCollection("xxx").then(
(value) => console.log(value);,
(error) => console.log("collection xxx may not exist.");
);
// 访问player collection
const col = db.collection('player');
// 插入
await col.insertMany([name: "as", sex: "a", name: "bx", sex: "b"]).then(
function(value)
console.log(value.insertedIds);
,
function(error)
console.log(error);
);
// 输出
col.find().toArray(function(err, items)
console.log(items);
);
// 删除
col.deleteOne(name:'as');
col.find().toArray(function(err, items)
console.log(items);
);
// 修改
col.updateMany(name:'bx', $set: name: "xixixi");
col.find().toArray(function(err, items)
console.log(items);
);
// 不存在则在插入document时自动创建该collection
const xxx = db.collection('xxx');
await xxx.insertMany([name: "x1", sex: "1", name: "x2", sex: "2"]).then(
function(value)
console.log(value.insertedIds);
,
function(error)
console.log(error);
);
// 查找
xxx.find().toArray(function(err, items)
console.log(items);
client.close();
);
);
5、也可以连接后,用另外的函数进行数据操作,操作时每一步await Promise的执行,最后close client connection
'use strict';
const MongoClient = require('mongodb');
function readConfig()
try
const config = require('./config/mongo.json');
return config;
catch (err)
// console.log(err);
process.exit(2);
const config = readConfig();
const port = config.port;
const host = config.host;
const url = `mongodb://$host:$port`;
const dbName = config.db;
const opts = config.opts;
// Connect using MongoClient
const client = new MongoClient(url, opts);
// CRUD
client.connect(async function(err, cli)
if (err) process.exit(5);
);
const db = client.db(dbName);
async function main()
const col = db.collection('player');
await col.insertMany([name: "bx", sex: "b"]).then(
function(value)
console.log(value.insertedIds);
,
function(error)
console.log(error);
);
await col.findOne().then(
function(value)
console.log(value);
,
function(error)
console.log(error);
);
return 'done.';
main()
.then(console.log)
.catch(console.error)
.finally(() => client.close());
以上是关于MongoDB使用mongodb driver api连接mongo并增删改查(mongodb module)的主要内容,如果未能解决你的问题,请参考以下文章
如何使`org.mongodb.driver.cluster`在spring boot中使用嵌入式mongodb?