用于Node.js的官方 MongoDB 驱动程序。在 mongodb-core 之上提供高级API,适用于最终用户。
注意:最近发布了v3.x,其中包含重大的API更改。你可以在这里找到更改列表 。
认为你发现了一个错误?想要在node-mongodb-native中看到新功能?请在我们的问题管理工具JIRA:
node-mongodb-native
JIRA中针对所有驱动程序项目(即NODE,PYTHON,CSHARP,JAVA)和核心服务器(即SERVER)项目的错误报告是 public 。
有关Node.js驱动程序的问题,疑问或反馈,请查看我们的支持渠道。请不要直接向任何驱动程序开发人员发送问题或问题 - 您更有可能在Google网上论坛上的 mongodb-user 列表中获得答案。
更改历史记录可以在 HISTORY.md 中找到。
HISTORY.md
有关版本兼容性矩阵,请参阅以下链接:
开始使用Node.js 3.0驱动程序的推荐方法是使用npm(节点包管理器)在项目中安装依赖项。
npm
鉴于您已使用npm init创建了自己的项目,我们通过执行以下npm命令来安装MongoDB驱动程序及其依赖项。
npm init
npm install mongodb --save
这将下载MongoDB驱动程序并在您的package.json文件中添加依赖项。
package.json
您也可以使用 Yarn 包管理器。
MongoDB驱动程序依赖于其他几个软件包。这些是:
kerberos程序包是C ++扩展,需要在系统上安装构建环境。您必须能够自己构建Node.js才能编译和安装kerberos模块。此外,kerberos模块需要MIT Kerberos包才能在UNIX操作系统上正确编译。有关要安装的库,请咨询UNIX操作系统包管理器。
kerberos
Windows已包含用于Kerberos身份验证的SSPI API。但是,您需要使用Visual Studio C ++安装完整的编译器工具链才能正确安装Kerberos扩展。
如果您没有build-essentials,则不会构建此模块。对于Linux,你需要gcc,g ++,Node.js以及所有头文件和Python。找出缺少的最简单的方法是尝试构建Kerberos项目。您可以通过执行以下步骤来执行此操作。
git clone https://github.com/mongodb-js/kerberos cd kerberos npm install
如果完成所有步骤,则表明您已安装正确的工具链。如果您收到错误“node-gyp not found,”您需要在全球范围内安装node-gyp:
node-gyp
npm install -g node-gyp
如果它正确编译并运行测试你是金色的。我们现在可以尝试通过执行以下命令安装mongod驱动程序。
mongod
cd yourproject npm install mongodb --save
如果仍然失败,则下一步是检查npm日志。重新运行命令,但在这种情况下是详细模式。
npm --loglevel verbose install mongodb
这将打印出在尝试安装模块时npm正在执行的所有步骤。
已知用于在Windows上编译kerberos的编译器工具链如下所示。
打开Visual Studio命令提示符。确保node.exe在您的路径中并安装node-gyp。
node.exe
接下来,您必须手动构建项目以对其进行测试。克隆存储库,安装依赖项并重建:
git clone https://github.com/christkv/kerberos.git cd kerberos npm install node-gyp rebuild
如果您已正确设置所有内容,则应成功重建驱动程序。
您的Python安装可能需要进行gyp break。首先通过尝试在有问题的服务器上构建Node.js来测试您的部署环境,因为这应该能够解决破坏软件包的任何问题(并且有很多破解的软件包)。
另一个提示是确保您的用户具有对安装Node.js模块的任何位置的写入权限。
本指南将向您展示如何使用Node.js和MongoDB设置简单的应用程序。它的范围只是如何设置驱动程序和执行简单的CRUD操作。有关更深入的报道,请参阅教程。
首先,创建一个应用程序所在的目录。
mkdir myproject cd myproject
输入以下命令并回答问题以创建新项目的初始结构:
接下来,安装驱动程序依赖项。
您应该看到 NPM 下载了大量文件。完成后,您将在 node_modules 目录下找到所有下载的软件包。
有关完整的MongoDB安装说明,请参见手册。
mongod --dbpath=/data
您应该看到 mongod 进程启动并打印一些状态信息。
创建一个新的 app.js 文件并添加以下代码以使用MongoDB驱动程序尝试一些基本的CRUD操作。
添加代码以连接到服务器和数据库 myproject :
const MongoClient = require('mongodb').MongoClient; const assert = require('assert'); // Connection URL const url = 'mongodb://localhost:27017'; // Database Name const dbName = 'myproject'; // Use connect method to connect to the server MongoClient.connect(url, function(err, client) { assert.equal(null, err); console.log("Connected successfully to server"); const db = client.db(dbName); client.close(); });
从命令行运行您的应用程序:
node app.js
应用程序应将已成功连接到服务器连接到控制台。
添加到 app.js 以下函数使用 insertMany 方法将3个文档添加到文档集合中。
const insertDocuments = function(db, callback) { // Get the documents collection const collection = db.collection('documents'); // Insert some documents collection.insertMany([ {a : 1}, {a : 2}, {a : 3} ], function(err, result) { assert.equal(err, null); assert.equal(3, result.result.n); assert.equal(3, result.ops.length); console.log("Inserted 3 documents into the collection"); callback(result); }); }
insert 命令返回包含以下字段的对象:
添加以下代码以调用 insertDocuments 函数:
const MongoClient = require('mongodb').MongoClient; const assert = require('assert'); // Connection URL const url = 'mongodb://localhost:27017'; // Database Name const dbName = 'myproject'; // Use connect method to connect to the server MongoClient.connect(url, function(err, client) { assert.equal(null, err); console.log("Connected successfully to server"); const db = client.db(dbName); insertDocuments(db, function() { client.close(); }); });
运行更新的 app.js 文件:
该操作返回以下输出:
Connected successfully to server Inserted 3 documents into the collection
添加一个返回所有文档的查询。
const findDocuments = function(db, callback) { // Get the documents collection const collection = db.collection('documents'); // Find some documents collection.find({}).toArray(function(err, docs) { assert.equal(err, null); console.log("Found the following records"); console.log(docs) callback(docs); }); }
此查询返回文档集合中的所有文档。将 findDocument 方法添加到 MongoClient.connect 回调:
const MongoClient = require('mongodb').MongoClient; const assert = require('assert'); // Connection URL const url = 'mongodb://localhost:27017'; // Database Name const dbName = 'myproject'; // Use connect method to connect to the server MongoClient.connect(url, function(err, client) { assert.equal(null, err); console.log("Connected correctly to server"); const db = client.db(dbName); insertDocuments(db, function() { findDocuments(db, function() { client.close(); }); }); });
添加查询过滤器以仅查找符合查询条件的文档。
const findDocuments = function(db, callback) { // Get the documents collection const collection = db.collection('documents'); // Find some documents collection.find({'a': 3}).toArray(function(err, docs) { assert.equal(err, null); console.log("Found the following records"); console.log(docs); callback(docs); }); }
只应返回与'a' : 3匹配的文档。
'a' : 3
以下操作更新文档集合中的文档。
const updateDocument = function(db, callback) { // Get the documents collection const collection = db.collection('documents'); // Update document where a is 2, set b equal to 1 collection.updateOne({ a : 2 } , { $set: { b : 1 } }, function(err, result) { assert.equal(err, null); assert.equal(1, result.result.n); console.log("Updated the document with the field a equal to 2"); callback(result); }); }
该方法通过向设置为 1 的文档添加新字段 b 来更新字段 a 等于 2 的第一个文档。接下来,从 MongoClient.connect 更新回调函数以包含更新方法。
const MongoClient = require('mongodb').MongoClient; const assert = require('assert'); // Connection URL const url = 'mongodb://localhost:27017'; // Database Name const dbName = 'myproject'; // Use connect method to connect to the server MongoClient.connect(url, function(err, client) { assert.equal(null, err); console.log("Connected successfully to server"); const db = client.db(dbName); insertDocuments(db, function() { updateDocument(db, function() { client.close(); }); }); });
删除字段 a 等于 3 的文档。
const removeDocument = function(db, callback) { // Get the documents collection const collection = db.collection('documents'); // Delete document where a is 3 collection.deleteOne({ a : 3 }, function(err, result) { assert.equal(err, null); assert.equal(1, result.result.n); console.log("Removed the document with the field a equal to 3"); callback(result); }); }
将新方法添加到 MongoClient.connect 回调函数。
const MongoClient = require('mongodb').MongoClient; const assert = require('assert'); // Connection URL const url = 'mongodb://localhost:27017'; // Database Name const dbName = 'myproject'; // Use connect method to connect to the server MongoClient.connect(url, function(err, client) { assert.equal(null, err); console.log("Connected successfully to server"); const db = client.db(dbName); insertDocuments(db, function() { updateDocument(db, function() { removeDocument(db, function() { client.close(); }); }); }); });
索引可以提高应用程序的性能。以下函数在文档集合中的 a 字段上创建索引。
const indexCollection = function(db, callback) { db.collection('documents').createIndex( { "a": 1 }, null, function(err, results) { console.log(results); callback(); } ); };
将indexCollection方法添加到您的应用程序:
indexCollection
const MongoClient = require('mongodb').MongoClient; const assert = require('assert'); // Connection URL const url = 'mongodb://localhost:27017'; const dbName = 'myproject'; // Use connect method to connect to the server MongoClient.connect(url, function(err, client) { assert.equal(null, err); console.log("Connected successfully to server"); const db = client.db(dbName); insertDocuments(db, function() { indexCollection(db, function() { client.close(); }); }); });
有关更多详细信息,请参阅教程。
Apache 2.0
©2009-2012 Christian Amor Kvalheim ©2012-present MongoDB 撰稿人