简体   繁体   English

Node.js可以从Meteor mini mongo检索数据吗?

[英]Can Nodejs retrieve data from Meteor mini mongo?

I'm trying to use Nodejs to get data from Meteorjs mini mongo database. 我正在尝试使用Nodejs从Meteorjs迷你mongo数据库中获取数据。 Here is my code: 这是我的代码:

var MongoClient = require('mongodb').MongoClient;

MongoClient.connect('mongodb://127.0.0.1:3001/meteor', function(err, db) {
  if(err) throw err;
  console.log("Connected to Database");
  var test = db.collection("apps");
  test.insert({"_id":"selfDefinedID"}, function(err,docs){
    console.log("docs inserted");
    console.log(docs);
  });
  test.find({"_id":"selfDefinedID"}).toArray(function(err,docs){
    console.log("docs founded");
    console.log(docs);
  });
});

Insert data works fine. 插入数据工作正常。 But, I can't retrieve data from meteor mini mongo database. 但是,我无法从流星迷你mongo数据库中检索数据。 And I got an error: 我得到一个错误:

{ [MongoError: Connection Closed By Application] name: 'MongoError' }

Is it possible to retrieve Meteor mini mongo data using Nodejs? 是否可以使用Nodejs检索Meteor mini mongo数据? If possible, how? 如果可能的话,怎么办?

The database meteor uses is a normal mongo database. 流星使用的数据库是普通的mongo数据库。 You can connect to it like any other mongo db. 您可以像连接其他任何mongo数据库一样连接到它。 If you are still in development mode, then the database runs at mongodb://localhost:3001/meteor , otherwise, in a bundled app, it's just the db you specified yourself using MONGO_URL . 如果您仍处于开发模式,则数据库运行在mongodb://localhost:3001/meteor ,否则,在捆绑的应用程序中,它只是您使用MONGO_URL指定的数据库。

On a windows machine, I created a new, blank, meteor project, and started it up. 在Windows机器上,我创建了一个新的空白流星项目,然后启动了它。 I then created a test script, npm installed the mongodb library, and ran your script and it worked fine. 然后,我创建了一个测试脚本,npm安装了mongodb库,并运行了脚本,它运行良好。 First run I get "docs inserted" and "docs founded", on subsequent runs obviously the insert is failing, but the find still works. 第一次运行时,我得到了“插入的文档”和“建立了文档”,在随后的运行中,插入显然失败了,但是查找仍然有效。

So two questions, first is this the same script your getting the error with? 有两个问题,首先是您遇到错误的同一个脚本吗? And two, if you create a blank meteor project and try it do you get the same error? 第二,如果您创建一个空白的流星项目并尝试使用它,是否会遇到相同的错误?

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

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