MongoDB查找查询未返回所有值 [英] MongoDB Find Query Not Returning All Values

查看:341
本文介绍了MongoDB查找查询未返回所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个传感器值的mongoDB集合,我试图在该集合中查找某个日期范围内的所有文档.我已经阅读了文档,并且实际上已经实现了代码以查找所有数据.但是,我随后将所有文档写入CSV文件,以便可以从网络上下载它们.我遇到的问题是查询似乎正在寻找正确日期范围内的所有数据(我已经在服务器端和客户端同时打印了日期,并且都匹配了正确的日期范围)但是,CSV文件仅返回大约一半的数据(不一致). 例如,假设我要查询集合以返回存储在以下之间的所有文档:

I have a mongoDB collection of sensor values where I'm trying to find all the documents in the collection that are within a certain date range. I've read the documents and have actually implemented the code to find all of the data. However, I'm then writing all of the documents to a CSV file so that I can download them from the web. The issue I'm running into is that the query seems to be looking for all of the data within the correct date range (I've printed the dates on both the server and client sides and both match up as to the correct date range) however, the CSV file only returns about half of the data (it's never consistent). For example, let's say I'm querying my collection to return all the documents stored between:

(2013年5月9日星期四07:57:06 GMT-0400)和(2013年5月14日星期二00:40:43 GMT-0400)

(Thu May 09 2013 07:57:06 GMT-0400) and (Tue May 14 2013 00:40:43 GMT-0400)

当我单击下载数据"按钮时,它将向服务器发送请求以查找该范围内的所有文档,然后将这些文档流式传输为文本文件并将其提供给客户端.我将控制台和服务器上的日期都打印到控制台上,并且所有内容都匹配...所以我很确定mongodb find函数在其查询中使用了正确的日期.

When I click the download data button, it sends a request to the server to find all the documents within this range and then stream the documents to a text file and serves it back to the client. I print to the console the dates on both the server and client, and everything matches... so I'm pretty sure the mongodb find function is using the correct dates in its query.

但是,当我打开此查询的CSV文件时,它为我提供了一份文档列表,其范围从(2013年5月9日星期四07:57:15->第一个日期输入gte到开始日期)...但只会将值返回到(Sat May 11 2013 01:04:09).这大约是三天的价值,该数据似乎从查询中丢失.由于我的传感器每8秒将数据上传到集合中,因此这是成千上万的条目.使用find函数可以检索的文档数量是否有某种数据限制? CSV文件几乎有6,850个条目(在Thu至Sat之间)...但是为什么不返回Thu至Tue之间的值的完整列表?我已经检查了数据库,并知道它在此时间范围内一直存储数据……所以我认为这不是问题.有人有什么想法吗?

However, when I open up the CSV file for this query, it gives me a list of documents spanning from (Thu May 09 2013 07:57:15 -> the first date entry gte to the start date)... but only returns values to (Sat May 11 2013 01:04:09). This is about three days worth of data that seems to be missing from the query. Since my sensors are uploading data to the collection every 8 seconds, this is thousands of entries. Is there some sort of data limit on the amount of documents that can be retrieved using the find function? The CSV file has almost 6,850 entries (between Thu to Sat)... but why wouldn't it be returning the full list of values between Thu to Tue? I've checked the database and know it has stored data consistently during this time range... so I don't think that would be the issue. Does anyone have any ideas?

这是我的查找功能的代码:

Here is the code for my find function:

collection.find({"addr": Number(json.addr), 
                 "Time": {$gte:new Date(json.startDate), 
                 $lte:new Date(json.endDate)}},
                 proj,
                 function (err, docs) 
                 {
                   if( err || !docs) console.log("No data found");
                   else 
                   {
                     var tempname=json.type+".csv";
                     var stream=fs.createWriteStream("pages/"+tempname);
                     stream.on('error', function(e){console.error(e);});

                     docs.each(function(err2, doc) 
                     {
                       if (!doc) 
                       {
                         console.log("done");
                         stream.end();
                         res.writeHead(200, {"Content-Type": "application/json"});
                         res.write(JSON.stringify({fname:tempname}));
                         res.end();
                         return;
                       }
                       stream.write(doc.Time+","+doc[json.type]+"\n");
                     });
                   }
                });

推荐答案

stream.write() 在缓冲区已满时返回false,因此您需要检查一下.

stream.write() returns false when the buffer is full, so you need to check for that.

此外,请参见> [使用Node.js编写大文件] 更多信息.

Also, see [Writing large files with Node.js] for more info.

这篇关于MongoDB查找查询未返回所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆