首页 > 数据库 >MongoDB Node.js Driver and MongoClient All In One

MongoDB Node.js Driver and MongoClient All In One

时间:2023-10-10 13:11:05浏览次数:36  
标签:Node MongoDB require Driver db mongoclient https mongodb MongoClient

MongoDB Node.js Driver and MongoClient All In One

The next generation Node.js driver for MongoDB

$ npm i mongodb
# OR
$ npm i -S mongodb
# OR
$ npm install mongodb --save

https://mongodb.github.io/node-mongodb-native/index.html

https://www.mongodb.com/docs/drivers/node/current/

https://learn.mongodb.com/learning-paths/using-mongodb-with-nodejs

https://mongodb.github.io/node-mongodb-native/Next/

image

The URL connection format

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

// new MongoClient

https://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html#the-url-connection-format

https://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html

demos

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

// MongoClient.connect
MongoClient.connect("mongodb://localhost:27017/integration_test", function(err, db) {
  test.equal(null, err);
  test.ok(db != null);

  db.collection("replicaset_mongo_client_collection").update({a:1}, {b:1}, {upsert:true}, function(err, result) {
    test.equal(null, err);
    test.equal(1, result);

    db.close();
    test.done();
  });
});

var Db = require('mongodb').Db,
    MongoClient = require('mongodb').MongoClient,
    Server = require('mongodb').Server,
    ReplSetServers = require('mongodb').ReplSetServers,
    ObjectID = require('mongodb').ObjectID,
    Binary = require('mongodb').Binary,
    GridStore = require('mongodb').GridStore,
    Grid = require('mongodb').Grid,
    Code = require('mongodb').Code,
    BSON = require('mongodb').pure().BSON,
    assert = require('assert');

  // Set up the connection to the local db ✅
  var mongoclient = new MongoClient(new Server("localhost", 27017), {native_parser: true});

  // Open the connection to the server
  mongoclient.open(function(err, mongoclient) {

    // Get the first db and do an update document on it
    var db = mongoclient.db("integration_tests");
    db.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) {
      assert.equal(null, err);
      assert.equal(1, result);

      // Get another db and do an update document on it
      var db2 = mongoclient.db("integration_tests2");
      db2.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) {
        assert.equal(null, err);
        assert.equal(1, result);

        // Close the connection
        mongoclient.close();
      });
    });
  });

(

标签:Node,MongoDB,require,Driver,db,mongoclient,https,mongodb,MongoClient
From: https://www.cnblogs.com/xgqfrms/p/17754405.html

相关文章

  • MongoDB可视化管理工具-MongoDB Compass【转】
    一、引言在使用MongoDB过程中,如果单单依靠命令行操作MongoDB数据库,效率不高而且查看不方便。因此MongoDB官网提供的一个可视化管理工具,叫MongoDBCompass,它集创建数据库、管理集合和文档、运行临时查询、评估和优化查询、性能图表、构建地理查询等功能为一体,很方便。二、......
  • nodejs之常用命令
    node控制台命令#新建文件夹mkdirname#清空控制台clearclr#跳转到指定目录cdname(目录名)#返回上一级cd..#移除指定文件rm文件名name#重命名renamesrc/index.htmlsrc/index.pug#清除缓存:cnpmcacheclean--forces#结束退出ctrl+c#......
  • MongoDB常用查询
    1.数据库数据说明#集合:school#文档:stu#结合字段:id,学号、姓名、电话、性别、年龄、学历、备注#初始化20条数据useschoolfor(varnum=1;num<=20;num++){db.stu.insert({id:num,no:"SN"+num,name:"name"+num,tel:"111......
  • MongoDB基础知识
    1.简介MongoDB官方文档菜鸟教程1、NoSQL(NotOnlySQL),不仅仅是SQL,主要是指非关系型数据库,是对不同与传统的关系型数据库的数据管理系统的统称2、NoSQL用于超大规模数据的存储,这些类型的数据存储吧需要固定的模式,无需多余的操作就可以横向扩展1.2NoSQL和RDBMS的区分......
  • MongoDB下载安装入门
    MongoDB下载安装入门一.MongoDB下载安装mongodb官网下载不了,MongoDB下载、安装、配置、使用,如何下载MongoDB数据库,MongoDB入门-CSDN博客按照文章一→六:安装,下载,环境变量配置等等MongoDBv4.2版安装目录:C:\ProgramFiles\MongoDB\Server\4.2\bin二.安全认证注意!!!一定要......
  • 【node爬虫】node爬虫实用教程
    准备工作通过指令npminit初始化文件夹,会获得package.json项目说明书。爬虫必备工具:cheerio;通过在终端输入npmicheerio,即可将文件装到项目里。cheerio 是 jquery 核心功能的一个快速灵活而又简洁的实现,主要是为了用在服务器端需要对 DOM 进行操作的地方。大家可以简......
  • mongodb慢查询对内存和CPU的影响
    所得结果均为ChatGPT所得,只是用来记录好复习对内存的影响数据加载到内存:MongoDB使用内存来缓存最频繁访问的数据,以提高查询性能。这个缓存通常称为"工作集"。当一个查询需要访问某些数据时,MongoDB会尝试从内存中获取数据,这比从磁盘读取数据要快得多。慢查询导致数据逐出:当......
  • 基于Node.js的大文件分片上传
    我们在做文件上传的时候,如果文件过大,可能会导致请求超时的情况。所以,在遇到需要对大文件进行上传的时候,就需要对文件进行分片上传的操作。同时如果文件过大,在网络不佳的情况下,如何做到断点续传?也是需要记录当前上传文件,然后在下一次进行上传请求的时候去做判断。前端1.index.ht......
  • 【最佳实践】MongoDB导出导入数据
    首先说一下这个3节点MongoDB集群各个维度的数据规模:1、dataSize:1.9T2、storageSize:600G3、全量备份-加压缩开关:186G,耗时8h4、全量备份-不加压缩开关:1.8T,耗时4h27m具体导出的语法比较简单,此处不再赘述,本文重点描述导入的优化过程,最后给出导入的最佳实践。■2023-09-13......
  • node-oracledb nodejs 包支持thin 模式了
    node-oracledb从v6.0.0版本来时支持thin模式了,是基于纯javascript开发的驱动,我们终于可以不用依赖比较笨重的oracleclient了属于一个很大的进度,同时python版本也是支持thin模式了,我以前也简单介绍过参考资料https://github.com/oracle/node-oracledb/releaseshttps://me......