Node操作Mongodb

1.Node连接数据库

  1. // 导入mongodb中的链接对象
  2. const MongoClient = require('mongodb').MongoClient;
  3. // 数据库服务器地址
  4. const url = 'mongodb://localhost:27017';
  5. // 数据库的名字
  6. const dbName = 'test';
  7. // 连接数据库服务器
  8. MongoClient.connect(url, function(err, client) {
  9. //连接数据库
  10. const adminDb = client.db(dbName);
  11. // 关闭数据库的连接
  12. client.close();
  13. });
  14. });

2. 查询数据库中的内容

  1. // 导入mongodb中的链接对象
  2. const MongoClient = require('mongodb').MongoClient
  3. // 数据库服务器地址
  4. const url = 'mongodb://localhost:27017';
  5. // 数据库的名字
  6. const dbName = 'test';
  7. // 连接数据库服务器
  8. MongoClient.connect(url, function(err, client) {
  9. //连接数据库以及数据库中的数据集合
  10. const col = client.db(dbName).collection("student");
  11. // 查询数据
  12. col.find({}).toArray(function(err,result){
  13. // 数据库查询错误
  14. if(err) throw err;
  15. // 查询成功返回的数据
  16. console.log(result)
  17. /*
  18. [
  19. {
  20. _id: 5eb50d7aec15f35088a31ac6,
  21. username: 'aa',
  22. age: 18
  23. },
  24. {
  25. _id: 5eb50d7aec15f35088a31ac7,
  26. username: 'bb',
  27. age: 30
  28. },
  29. {
  30. _id: 5eb50d7aec15f35088a31ac8,
  31. username: 'cc',
  32. age: 22
  33. }
  34. ]
  35. */
  36. })
  37. // 关闭数据库的连接
  38. client.close();
  39. });

3. 向数据库中插入内容

  1. // 导入mongodb中的链接对象
  2. const MongoClient = require('mongodb').MongoClient
  3. // 数据库服务器地址
  4. const url = 'mongodb://localhost:27017';
  5. // 数据库的名字
  6. const dbName = 'test';
  7. // 连接数据库服务器
  8. MongoClient.connect(url, function(err, client) {
  9. //连接数据库以及数据库中的数据集合
  10. const col = client.db(dbName).collection("student");
  11. // 像数据库中插入数据
  12. col.insert([
  13. {
  14. username:"aa",
  15. age:18
  16. },
  17. {
  18. username:"bb",
  19. age:20
  20. },
  21. {
  22. username:"cc",
  23. age:20
  24. }
  25. ], function(err, result){
  26. // 如果数据插入错误则抛出错误
  27. if(err) throw err
  28. // result 数据插入成功后返回的内容
  29. console.log(result)
  30. /*
  31. {
  32. result: { ok: 1, n: 3 },
  33. ops: [ [Object], [Object], [Object] ],
  34. insertedCount: 3, // 插入几条数据
  35. insertedIds: { // 数据的id
  36. '0': 5eb50d7aec15f35088a31ac6,
  37. '1': 5eb50d7aec15f35088a31ac7,
  38. '2': 5eb50d7aec15f35088a31ac8
  39. }
  40. }
  41. */
  42. })
  43. // 关闭数据库的连接
  44. client.close();
  45. });

4. 更新数据

  1. // 导入mongodb中的链接对象
  2. const MongoClient = require('mongodb').MongoClient
  3. // 数据库服务器地址
  4. const url = 'mongodb://localhost:27017';
  5. // 数据库的名字
  6. const dbName = 'test';
  7. // 连接数据库服务器
  8. MongoClient.connect(url, function(err, client) {
  9. //连接数据库以及数据库中的数据集合
  10. const col = client.db(dbName).collection("student");
  11. // 更新数据
  12. student.update({
  13. age:22
  14. },{$set:{sex:"女"}},{multi:true}, function(err,results){
  15. // 如果数据更新错误则抛出错误
  16. if(err) throw err
  17. // 更新数据库成功后返回的内容
  18. console.log(results)
  19. /*
  20. {
  21. result: { n: 2, nModified: 2, ok: 1 },
  22. }
  23. */
  24. })
  25. })
  26. // 关闭数据库的连接
  27. client.close();
  28. });

5. 删除数据

  1. // 导入mongodb中的链接对象
  2. const MongoClient = require('mongodb').MongoClient
  3. // 数据库服务器地址
  4. const url = 'mongodb://localhost:27017';
  5. // 数据库的名字
  6. const dbName = 'test';
  7. // 连接数据库服务器
  8. MongoClient.connect(url, function(err, client) {
  9. //连接数据库以及数据库中的数据集合
  10. const col = client.db(dbName).collection("student");
  11. // 删除数据
  12. col.deleteOne({
  13. age: 30
  14. }, function(err,results){
  15. // 如果数据删除错误则抛出错误
  16. if(err) throw err
  17. // 删除数据库成功后返回的内容
  18. console.log(results)
  19. /*
  20. {
  21. result: { n: 1, ok: 1 },,
  22. }
  23. */
  24. })
  25. })
  26. })
  27. // 关闭数据库的连接
  28. client.close();
  29. });