首页 > 数据库 >node连接mongodb

node连接mongodb

时间:2023-05-31 12:05:53浏览次数:43  
标签:node const mongodb xxx console true 连接 MongoClient

主要试用了两个库:mongodb、mongoose


由于服务器使用的是比较老版本的mongodb,如果使用比较新的客户端,编译时会出现问题:

Server at xxxx:27017 reports maximum wire version 4, but this version of the Node.js Driver requires at least 6 (MongoDB 3.6)


终级解决方案:去掉类型


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

@types/mongodb 在package.json中去掉了,yarn install不会自动 删除,得手工删除了  rm -rf node_modules/@types/mongodb。 再./build.sh 才能成功……



mongoose

使用 5.13.17版本的mongoose搭配 typescript时,使用ts-node能直接运行成功。但是使用 tsc 进行编译时,出现各种问题……

原因可能是:当时不是原生支持typescript,类型定义不支持高版本的typescript?

废弃,使用mongodb重写


mongodb


import { MongoClient } from 'mongodb';
// or as an es module:
// import { MongoClient } from 'mongodb'

// Connection URL
const url = 'mongodb://xxxx:27017/xxx';
const client = new MongoClient(url, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  auth: {
    user: 'xxx',
    password: 'xxx'
  }
  // useFindAndModify: true
  // useCreateIndex: true
});

// Database Name
const dbName = 'xxx';

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 = await db.collection('deviceInfo').estimatedDocumentCount();

  console.log(collection);
  // the following code examples can be pasted here...

  return 'done.';
}

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




标签:node,const,mongodb,xxx,console,true,连接,MongoClient
From: https://blog.51cto.com/shoucuohulu/6385826

相关文章

  • 【windowns】记一次天玥安全网关无法连接运维机排查
    问题背景:在用天玥安全网关来连接项目中的运维机时,发现无法连接报网络连接错误(报错图如下:),但是通过同网络的区域的其它电脑是可以正常远程到目标主机。解决方案:经过排查发现,是因为之前有做加固操作,WindowsServer远程桌面SSL/TLS漏洞修复  修复过程如下:1、运行“gpedit.m......
  • 【分享】推荐一个非常好用的redis远程连接工具
    推荐一个非常好用的redis远程连接工具蓝奏云地址https://wwsi.lanzoum.com/ig7xZ0xspf0h密码:4vnz二维码:......
  • docker evel=error msg="error reading the kernel parameter net.ipv4.vs.expire_nod
    我使用的是dockerswarm-#报错evel=errormsg="errorreadingthekernelparameternet.ipv4.vs.expire_nodest_conn"error="open/proc/sys/net/ipv4/vs/expire_nodest_conn:nosuchfileordirectory"-#查看是否开启ip_vslsmod|grepip_vs==============......
  • MongoDB C++ gridfs worked example
    使用libmongoc,参考:http://mongoc.org/libmongoc/current/mongoc_gridfs_t.html#include<mongoc.h>#include<stdio.h>#include<stdlib.h>#include<fcntl.h>classMongoGridFS{public:MongoGridFS(constchar*db);~MongoGridFS();......
  • mongodb c++ driver安装踩坑记
     安装教程:https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/installation/(1)“initializer_list”filenotfoundhttp://stackoverflow.com/questions/19493671/initializer-list-no-such-file-or-directorySinceyouareusing GCC-4.8 andyourproblemisthatyoud......
  • Python连接es笔记三之es更新操作
    本文首发于公众号:Hunter后端原文链接:Python连接es笔记三之es更新操作这一篇笔记介绍如何使用Python对数据进行更新操作。对于es的更新的操作,不用到Search()方法,而是直接使用es的连接加上相应的函数来操作,本篇笔记目录如下:获取连接update()update_by_query()批量......
  • nmcli --- Linux下通过命令行管理WiFi连接
    1、建立和删除一个wifi连接创建wifi连接(SSID:hello,密码:12345678)的示例:$nmclidevicewificonnecthellopassword12345678每次命令执行后,会在/etc/NetworkManager/system-connections/目录下创建一个新文件hello来保存配置,重复执行则创建多个这样的文件。删除wifi连接的示例......
  • leetcode 671. Second Minimum Node In a Binary Tree
    Givenanon-emptyspecialbinarytreeconsistingofnodeswiththenon-negativevalue,whereeachnodeinthistreehasexactlytwoorzerosub-node.Ifthenodehastwosub-nodes,thenthisnode'svalueisthesmallervalueamongitstwosub-nodes.G......
  • 在node项目中使用log4.js记录日志
    1.在项目根目录创建保存日志文件的文件夹logs2.修改.gitignore文件,添加logs文件夹,这样使用git提交进忽略logs文件夹。node_modules.envlogs3.在config文件夹下新增log4j.js文件保存log4js的配置,路径:./src/config/log4j.js//config.jsletpath=require('pat......
  • 2023-05-30 前端通过node获取七牛云的token(token最好还是在后端返回,前端获取token会暴
    constfs=require('fs');constqiniu=require('qiniu');varaccessKey='你的accessKey';varsecretKey='你的secretKey';varmac=newqiniu.auth.digest.Mac(accessKey,secretKey);//获取七牛tokenvaroptions={......