简体   繁体   English

从MongoDB加载数据并使用cursorstream进行可视化

[英]Loading Data from MongoDB and using cursorstream for Visualization

I want to access real time data in MongoDB which will be updated online for map and graph visualization using javascript. 我想访问MongoDB中的实时数据,该数据将在线更新以使用JavaScript进行地图和图形可视化。 I try to follow the example in MongoDB website using cursorstream. 我尝试使用游标流在MongoDB网站中遵循示例。 The code is as shown below. 代码如下所示。 According to the example, this should work, but node does not recognize the .stream() part... 根据示例,这应该可行,但是节点无法识别.stream()部分。

    var databaseUrl = "127.0.0.1/sensor_db2"; 

var collections = ["sensor"]
var db = require("mongojs").connect(databaseUrl, collections);


var stream = db.sensor.find().stream();

stream.on("data", function(item) {
    console.log(item);
});


stream.on('error', function (err) {
  console.error(err);
});

Then I got the error message like this: 然后我得到了这样的错误消息:

    /Users/user/Documents/nodetest/nodetest.js:6
var stream = db.sensor.find().stream();
                              ^
TypeError: Object [object Object] has no method 'stream'
    at Object.<anonymous> (/Users/user/Documents/nodetest/nodetest.js:6:35)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3
/Users/user/Documents/nodetest/nodetest.js:6
var stream = db.sensor.find().stream();
                              ^
TypeError: Object [object Object] has no method 'stream'
    at Object.<anonymous> (/Users/user/Documents/nodetest/nodetest.js:6:35)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3
BlackBook:nodetest user$ node nodetest.js

I run it using node.js. 我使用node.js运行它。

Anyone can help what's wrong in it? 任何人都可以帮助解决其中的问题吗? Is there any other approach to achieve the same thing? 还有其他方法可以实现同一目标吗? (load data from mongodb and visualizes the data in a real-timechart)? (从mongodb加载数据并在实时图表中可视化数据)?

I haven't used mongojs before but it looks like your trying to cross streams with the native mongo driver, which offers a .stream() function on collections, and mongojs, which seems to offer it in a slightly different way. 我以前没有使用过mongojs,但似乎您尝试使用本机mongo驱动程序(在集合上提供.stream()函数)和mongojs(似乎以略有不同的方式提供)来跨流。 From mongojs' git hub page it looks like you can accomplish what you're trying to do with this. 在mongojs的git中心页面上,您似乎可以完成您要尝试执行的操作。

https://github.com/mafintosh/mongojs#streaming-cursors https://github.com/mafintosh/mongojs#streaming-cursors

first npm install JSONStream 首先npm安装JSONStream

var mongojs = require('mongojs');
var JSONStream = require('JSONStream');

var db = mongojs.connect(databaseUrl, collections);
// pipe all documents in mycollection to stdout
db.sensor.find({}).pipe(JSONStream.stringify()).pipe(process.stdout);

I ran this on a collection on my database and it worked. 我在数据库上的集合上运行了它,然后它起作用了。 Good luck with it. 祝你好运。 Hope it helped. 希望能有所帮助。

First of all, the reason you are getting that error is because of the .stream() you are attempting to use. 首先,出现该错误的原因是由于您尝试使用.stream()。 As the previous answer hinted to, you can use .pipe(). 正如前面的答案所暗示的,您可以使用.pipe()。

However, from what I read, you desire to start this little script up and be able to capture new inserts into the collection. 但是,从我阅读的内容中,您希望启动这个小脚本,并能够将新插入内容捕获到集合中。 You can do this relatively easily using a capped collection. 您可以使用上限集合相对容易地完成此操作。

To convert a non-capped collection to a capped collection, the command you issue is this: 要将非上限集合转换为上限集合,您发出的命令是这样的:

> db.runCommand({"convertToCapped": "sensor", size: 100000});

The size parameter is the size in bytes. size参数是字节大小。 The reason for using a capped collection is that they are tailable; 使用上限集合的原因是它们是可尾的。 thus when new documents are inserted into the DB, you are able to retrieve them. 因此,当将新文档插入数据库时​​,便可以检索它们。

So with a capped collection, you should be able to do something like so: 因此,使用封顶的集合,您应该可以执行以下操作:

var databaseUrl = "127.0.0.1/sensor_db2";
var collections = ["sensor"]
var db = require("mongojs").connect(databaseUrl, collections);


var stream = db.sensor.find({},{},{tailable:true, timeout:false});

stream.on("data", function(item) {
    console.log(item);
});


stream.on('error', function (err) {
  console.error(err);
});

You can find more information on capped collections on the mongodb docs page here . 你可以找到关于MongoDB的文档页面上加盖集合的详细信息在这里 Also the tailable cursor example from the mongojs github page is located here . 另外,位于mongojs github页面上的可尾光标示例也位于此处

I understand that you aren't using a capped collection, so this answer may not be your desired answer; 我知道您使用的不是上限,因此此答案可能不是您想要的答案; however, I'm putting this as an answer in case you can use a capped collection. 但是,我想以此为答案,以防您可以使用上限集合。

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

相关问题 使用D3.js加载本地数据以进行可视化 - Loading local data for visualization using D3.js 使用JUNG从MySQL检索数据和可视化 - Retrieving the data from MySQL and visualization using JUNG 无法使用Express NodeJS和MongoDB从API检索数据,正在加载 - Unable to retrieve data from API using Express NodeJS and MongoDB, loading 为 Titanic 可视化加载数据时遇到问题 - Trouble Loading Data for Titanic Visualization 从Rest api获取数据异步并使用node.js将它们加载到mongodb-使用Promise - get data asynchron from Rest api and loading them to mongodb with node.js - using promises Google Gauge(数据图表可视化)未加载到由ajax调用的页面中 - Google Gauge (Data Chart Visualization) not loading in page called by ajax 使用Google Analytics(G)API和Google Visualization - 显示数据,但是从错误的月份开始 - Using Google Analytics (G)API and Google Visualization - Showing data, but from the wrong month 在D3.js地图可视化中使用来自两个单独的geoJSON文件的数据 - Using data from two separate geoJSON files in D3.js map visualization 如何使用JavaScript加载可视化来控制进度元素? - How can I control a progress element using JavaScript for Loading visualization? 将data.group用于Google可视化仪表板 - Using data.group for Google Visualization Dashboards
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM