当前位置:   article > 正文

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

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

一、Node连接MongoDB

mongodb

npm install mongodb
# or ...
yarn add mongodb
  • 1
  • 2
  • 3

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());
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

在这里插入图片描述

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())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

在这里插入图片描述

在这里插入图片描述

二、使用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())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
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())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/548516
推荐阅读
相关标签
  

闽ICP备14008679号