天道酬勤,学无止境

node-mongodb-native

Cannot loop MongoDB Collection Functions

For some reason, I cannot loop collection.count. It keeps printing filename[5] 5 times instead of starting at 1 and going to 5. This makes no sense because I can manually copy and paste this function 5 times and it will work, but when in a for loop, it doesn't. for( var k =0; k<(filename.length)-2;k++) { collection.count( { "display.Name": filename[k] } , function(err, count) { console.log("Filename: " +filename[k]); console.log(" Trips: " + count); }); } NOTE: If I put the console.log outside of collection.count, I will see all 5 of my filenames. It is only within the specific function that

2022-01-17 17:21:35    分类:问答    node.js   node-mongodb-native

Node.JS + mongo:.find().each() 在第一批后停止(Node.JS + mongo: .find().each() stopping after first batch)

问题 这让我很难过。 我有一个独立的(命令行执行的)节点脚本,其目的是遍历一个大集合(几十万个)中的所有文档,并对每个文档执行一些计算,运行一些额外的 JS 代码,然后用一些新值更新文档。 根据cursor.each()的文档,一旦我从collection.find()获得光标, .each(cb)方法应该对整个集合中的每个项目执行cb(item) 。 示例代码: myDb.collection('bigcollection').find().each(function(err, doc) { if (err) { console.log("Error: " + err); } else { if (doc != null) { process.stdout.write("."); } else { process.stdout.write("X"); } } }); 我希望这样做是打印出几十万. 's 然后在最后打印一个X ,因为cursor.each()应该“迭代此游标的所有文档”,并且根据示例代码,“如果项目为空,则游标已用尽/为空并关闭。” 但它实际上做的是精确地打印出 101 . 's,最后没有X 如果我调整批量大小( .find().batchSize(10).each(... ),它会在保释之前准确地检查该数量的文档。 那么,为什么只处理第一批呢?

2022-01-15 20:55:55    分类:技术分享    javascript   node.js   mongodb   node-mongodb-native

Node.JS + mongo: .find().each() stopping after first batch

This has me stumped. I have a standalone (command-line executed) node script, whose purpose is to iterate through all the documents in a large collection (several hundred thousand of them), and for each document, perform a few calculations, run a little additional JS code, and then update the document with some new values. Per the documentation for cursor.each(), once I've got my cursor from collection.find(), the .each(cb) method should execute cb(item) on each item in the entire collection. Example code: myDb.collection('bigcollection').find().each(function(err, doc) { if (err) { console.log

2022-01-15 18:32:33    分类:问答    javascript   node.js   mongodb   node-mongodb-native

node-mongodb-native MongoClient 意外关闭连接(node-mongodb-native MongoClient unexpectedly closing connections)

问题 我一直在寻找 mongodb 中意外关闭的连接,但只能从希望关闭连接的人那里找到问题。 我正在使用 node-mongodb-native 连接到数据库,但我不断收到看似随机的“错误:连接关闭”消息。 如果我手动重试请求(浏览器刷新),则请求有效。 知道是什么原因造成的吗? 是否有一些简单的选项会有所帮助? 我正在使用以下方法获取我的数据库句柄: MongoClient.connect(connection_string, { auto_reconnect: true }, function (err, db) { //server code/routes in here } 我正在浏览 https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/connection/server.js 但我意识到我对连接池的管理方式的有限理解让我感到困惑向上。 我的印象是他们会在我的服务器的整个生命周期内保持打开状态。 有人可以帮忙吗? 编辑:阅读 mjhm 的评论后,我开始更深入地研究 TCP keep alive。 偶然发现一些网站暗示这可能是 Azure 所做的(这个问题现在被错误分类了!)。 显然,Azure 负载均衡器会在活动 1 分钟后终止连接。 我正在使用 Azure 网站

2022-01-11 16:04:14    分类:技术分享    node.js   mongodb   node-mongodb-native

node-mongodb-native MongoClient unexpectedly closing connections

I've been searching a lot for unexpectedly closed connections in mongodb but can only find questions from people who WANT their connections to close. I am using node-mongodb-native to connect to a db, but I keep getting seemingly random "Error: connection closed" messages. If I manually retry the request (browser refresh) the request works. Any idea what is causing this? Is there some simple option that will help? I'm getting my db handle using: MongoClient.connect(connection_string, { auto_reconnect: true }, function (err, db) { //server code/routes in here } I was looking through https:/

2022-01-08 13:34:27    分类:问答    node.js   mongodb   node-mongodb-native

Express.js - 在 URL 中过滤 mongodb id(Express.js - Filter a mongodb id in the URL)

问题 这个问题的灵感来自这篇文章,但在我的情况下,我需要过滤 MongoId。 是否可以轻松地进行过滤,因为我需要在每条路线中使用它? app.post('/:mongoId(^[0-9a-fA-F]{24}$)', function(req, res){ // Send query based on mongoId } 回答1 你快到了,只是不要添加^和$锚点。 而且大写的AF范围甚至不是必需的,因为 Express 似乎匹配不区分大小写: app.post('/:mongoId([0-9a-f]{24})', function(req, res){ var id = req.param('mongoId'); ... }); 回答2 根据 Express API 文档,是的,您可以使用正则表达式作为路径: 也可以使用正则表达式,如果您有非常具体的限制,它会很有用。 app.get(/^\/commits\/(\w+)(?:\.\.(\w+))?$/, function(req, res){ var from = req.params[0]; var to = req.params[1] || 'HEAD'; res.send('commit range ' + from + '..' + to); });

2021-12-10 06:43:42    分类:技术分享    node.js   mongodb   filter   express   node-mongodb-native

使用 mongoose、gridfs-stream 和 multer 上传文件时出现 MongoError(MongoError when uploading a file using mongoose, gridfs-stream and multer)

问题 我正在使用 multer、gridfs-stream 和 mongoose 和 mongodb 运行 express 4,我正在尝试上传文件并将其流式传输到 gridfs。 执行此操作的快速路由定义为: app.post('/uploadfile', function (req, res) { console.dir(req.files); // The mongodb instance created when the mongoose.connection is opened var db = mongoose.connection.db; // The native mongo driver which is used by mongoose var mongoDriver = mongoose.mongo; // Create a gridfs-stream var gfs = new Gridfs(db, mongoDriver); var file = req.files.myFile; var fileId = new ObjectId(); console.log("Creating WriteStream"); var writeStream = gfs.createWriteStream({ _id: fileId, filename: file

2021-11-28 23:58:05    分类:技术分享    node.js   mongoose   node-mongodb-native   gridfs-stream   multer

Express.js - Filter a mongodb id in the URL

This question inspired by this post but in my case I need to filter MongoId. Is it possible to make filtering easily that the below because I need use it in each route? app.post('/:mongoId(^[0-9a-fA-F]{24}$)', function(req, res){ // Send query based on mongoId }

2021-11-25 11:44:12    分类:问答    node.js   mongodb   filter   express   node-mongodb-native

MongoError when uploading a file using mongoose, gridfs-stream and multer

I am running express 4 using multer, gridfs-stream and mongoose with mongodb and I am attempting to upload a file and stream it to gridfs. The express route that does this is defined as: app.post('/uploadfile', function (req, res) { console.dir(req.files); // The mongodb instance created when the mongoose.connection is opened var db = mongoose.connection.db; // The native mongo driver which is used by mongoose var mongoDriver = mongoose.mongo; // Create a gridfs-stream var gfs = new Gridfs(db, mongoDriver); var file = req.files.myFile; var fileId = new ObjectId(); console.log("Creating

2021-11-22 02:59:27    分类:问答    node.js   mongoose   node-mongodb-native   gridfs-stream   multer

MongoDB 仅在存在时按数组属性过滤(MongoDB filter by array property only if it exists)

问题 我的数据库对象有一个名为tags的可选属性,它是一个字符串数组。 我想编写一个查询,如果它们匹配以下条件之一,则返回对象: 他们没有tags属性。 它们有一个 tags 属性,该属性至少有一个项目包含在另一个名为queryTags数组中 通过阅读文档,我想出了以下内容,但它不起作用: let query = { tags: { '$or': [{'$exists': false}, {'$in': queryTags}]} } 回答1 $or是顶级运算符,因此您的查询需要是: let query = { '$or': [{tags: {'$exists': false}}, {tags: {'$in': queryTags}}] }

2021-10-23 12:44:49    分类:技术分享    node.js   mongodb   node-mongodb-native