简体   繁体   English

MongoDB查找查询未返回所有值

[英]MongoDB Find Query Not Returning All Values

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. 我有一个传感器值的mongoDB集合,我试图在该集合中查找某个日期范围内的所有文档。 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. 但是,我随后将所有文档写入CSV文件,以便可以从网络上下载它们。 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). 我遇到的问题是查询似乎正在寻找正确日期范围内的所有数据(我已经在服务器端和客户端同时打印了日期,并且都匹配了正确的日期范围)但是,CSV文件仅返回大约一半的数据(不一致)。 For example, let's say I'm querying my collection to return all the documents stored between: 例如,假设我要查询我的集合以返回之间存储的所有文档:

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

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. 我将控制台和服务器上的日期都打印到控制台上,并且所有内容都匹配...所以我很确定mongodb find函数在其查询中使用了正确的日期。

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). 但是,当我打开此查询的CSV文件时,它为我提供了一份文档列表,其范围是(2013年5月9日星期四07:57:15->第一个日期输入项gte至开始日期)...但仅返回值(2013年5月11日星期六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. 由于我的传感器每8秒将数据上传到集合中,因此这是成千上万的条目。 Is there some sort of data limit on the amount of documents that can be retrieved using the find function? 使用find函数可以检索的文档数量是否有某种数据限制? 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? CSV文件有将近6,850个条目(在Thu至Sat之间)...但是为什么不返回Thu至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() returns false when the buffer is full, so you need to check for that. 当缓冲区已满时, stream.write()返回false,因此您需要检查一下。

Also, see [Writing large files with Node.js] for more info. 另请参阅[使用Node.js编写大文件]了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM