【Node.js工程师养成计划】之使用Node连接MongoDB进行增删改查

一、Node连接MongoDB

mongodb

npm install mongodb
# or ...
yarn add mongodb

demo:

const { MongoClient } = require('mongodb');
// or as an es module:
// import { MongoClient } from 'mongodb'

// Connection URL
const url = 'mongodb://localhost:27017';
const client = new MongoClient(url);

// Database Name
const dbName = 'myProject';

async function main() {
  // Use connect method to connect to the server
  await client.connect();
  console.log('Connected successfully to server');
  const db = client.db(dbName);
  const collection = db.collection('documents');

  // the following code examples can be pasted here...

  return 'done.';
}

main()
  .then(console.log)
  .catch(console.error)
  .finally(() => client.close());

在这里插入图片描述

const { MongoClient} = require('mongodb')

const url = 'mongodb://127.0.0.1:27017'
const client = new MongoClient(url)

const dbName = 'test';

async function main() {
  await client.connect();
  console.log('Connected successfully to server')
  const db = client.db(dbName)
  const user = db.collection('user')
  const d = await user.find()
  console.log(await d.toArray())
}

main()
  .then(console.log)
  .catch(console.error)
  .finally(() => client.close())

在这里插入图片描述

在这里插入图片描述

二、使用Node进行增删改查

const { MongoClient} = require('mongodb')

const url = 'mongodb://127.0.0.1:27017'
const client = new MongoClient(url)

const clientFun = async function(c){
  await client.connect()
  console.log('连接成功')
  const db = client.db('test')
  return db.collection(c)
}

async function main() {
  const user = await clientFun('user')
  const d = await user.find()
  console.log(await d.toArray())
}

main()
  .then(console.log)
  .catch(console.error)
  .finally(() => client.close())
const { MongoClient} = require('mongodb')

const url = 'mongodb://127.0.0.1:27017'
const client = new MongoClient(url)

const clientFun = async function(c){
  await client.connect()
  console.log('连接成功')
  const db = client.db('test')
  return db.collection(c)
}

async function main() {
  const user = await clientFun('user')

  // 查询
  // const d = await user.find()
  // const d = await user.findOne({age: {$gt: 18}})
  // const d = await user.find({age: {$gt: 18}})
  // console.log(await d.toArray())

  // 插入
  // const d = await user.insertOne({name: 'aa', age: 6})
  // const d = await user.insertMany([
  //   {name: 'bb', age: 11},
  //   {name: 'cc', age:22}
  // ])
  // console.log(d)

  // 更新
  // const d = await user.updateOne({name: 'Alen111'}, {$set: {name: 'dd'}})
  // console.log(d)

  // 删除
  const d = await user.deleteOne({name: 'Alen1'})
  console.log(d)
}

main()
  .then(console.log)
  .catch(console.error)
  .finally(() => client.close())
  • 13
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老电影故事

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值