如何在不使用官方 MongoDB Node.js 驱动程序的情况下使用本机 nodejs 连接到 mongodb 数据库?

How can i connect to mongodb database with native nodejs without using the official MongoDB Node.js Driver?

我通过 google 搜索了很多答案,但所有建议都是通过 npm 安装 mongodb。然后我试图分析MongoDB Node.js 驱动程序源代码,mongodb library 在哪里。并且发现好像是通过nodejs原生的net模块来实现的。谁能给我指点一下,谢谢你的建议。

您可以使用您最喜欢的套接字库连接到 mongod/mongos 的端口并直接使用原始协议。握手描述为here. OP_MSG which is used with 3.6 and newer servers is described here. See also this。您可能需要阅读一些驱动程序源代码以了解如何将所有内容连接在一起(例如,消息需要序列化为 bson 以便通过网络传输)。