简体   繁体   English

Node.js和mongodb访问mongodb

[英]Node.js and mongodb access mongodb

I'm trying to set up mongodb on Windows 8 using node.js, Does anyone know why im getting this error. 我正在尝试使用node.js在Windows 8上设置mongodb,有人知道为什么我会收到此错误。 C:\\users\\phill\\node_modules\\mongodb\\lib\\mongodb\\mongo_client.js:359 it also says at collection = db collection,,, can't call method 'collection' of null. C:\\ users \\ phill \\ node_modules \\ mongodb \\ lib \\ mongodb \\ mongo_client.js:359也说在collection = db collection时,不能调用null的方法“ collection”。 I'm having a hard time setting it up. 我很难设置它。 My goal is to be able to add to mongo db, and see that I add or pull up what I added, but adding something is good enough for me for now. 我的目标是能够添加到mongo db,并看到我添加或提取了我添加的内容,但是现在添加一些东西对我来说已经足够了。 I'm trying every thing I can find, even straight from the website, I tried everything I see on here as well. 我正在尝试我能找到的所有东西,甚至直接从网站上,也尝试了我在这里看到的所有东西。 Think it maybe it's the way I have things set up. 认为这可能是我设置事情的方式。 My node.js is saved in my c: drive there is a file that says, program files(86x) in there I have node_modules, npm and such. 我的node.js保存在我的c:驱动器中,有一个文件说,其中的程序文​​件(86x)有node_modules,npm等。 The path ends up being, computer > windows (C:) > program files(86x) > nodejs. 路径最终是,计算机> Windows(C :)>程序文件(86x)> nodejs。 My Mongodb is saved right on my C: drive the path end up being windows (C:) > mongodb-win32-x86_64-2008plus-2.4.8. 我的Mongodb保存在我的C:驱动器上,路径最终是Windows(C :)> mongodb-win32-x86_64-2008plus-2.4.8。 In my C: I also created a file data and in it created another db. 在我的C语言中:我还创建了一个文件数据,并在其中创建了另一个数据库。 I have been told i should just use mongoose, I'm just learning so i open to any advice, links or anything that will help. 我被告知我应该只使用猫鼬,我只是在学习,所以我愿意接受任何建议,链接或任何有帮助的东西。 I have one last question as well, i learned php and then found out about sql injections and stuff like that, i am not seeing anything about security at all, should i expect the same as well. 我还有最后一个问题,我学习了php,然后发现了有关sql注入和类似的东西,我完全没有看到任何有关安全性的信息,我是否也希望得到同样的结果。 For this i get text not defined, but i have been getting errors with everthing i have done, best i did was get stuck on a right concern screen. 为此,我得到未定义的文本,但是我所做的一切都出现错误,最好的解决方法是卡在正确的关注屏幕上。

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

MongoClient.connect("mongodb://localhost:27017/integration_test", function(err, db) {
test.equal(null, err);
test.ok(db != null);

db.collection("replicaset_mongo_client_collection").update({a:1},          
{b:1},  {upsert:true},          function(err, result) {
test.equal(null, err);
test.equal(1, result);

db.close();
test.done();
});
});

Tried this as well and getting a error,C:\\users\\phill\\node_modules\\mongodb\\lib\\mongodb\\mongo_client.js:359.... at collection = db collection,,, can't call method 'collection' of null. 也尝试过此操作并收到错误,C:\\ users \\ phill \\ node_modules \\ mongodb \\ lib \\ mongodb \\ mongo_client.js:359 .... at collection = db collection,无法调用以下方法的“ collection”空值。 im calling it in command prompt node filename.js I'm saving it where my node.js file is, I have pulled up files before and created a server. 我在命令提示符节点filename.js中调用它。我将其保存在我的node.js文件所在的位置,我之前已拉出文件并创建了服务器。

var Db = require('mongodb').Db,
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
ReplSetServers = require('mongodb').ReplSetServers,
ObjectID = require('mongodb').ObjectID,
Binary = require('mongodb').Binary,
GridStore = require('mongodb').GridStore,
Grid = require('mongodb').Grid,
Code = require('mongodb').Code,
BSON = require('mongodb').pure().BSON,
assert = require('assert');

var db = new Db('test', new Server('localhost', 27017));
// Fetch a collection to insert document into
db.open(function(err, db) {
var collection = db.collection("simple_document_insert_collection_no_safe");
// Insert a single document
collection.insert({hello:'world_no_safe'});

// Wait for a second before finishing up, to ensure we have written the item to disk
setTimeout(function() {

// Fetch the document
collection.findOne({hello:'world_no_safe'}, function(err, item) {
assert.equal(null, err);
assert.equal('world_no_safe', item.hello);
db.close();
})
}, 100);
});

In your first code example, you said: 在第一个代码示例中,您说:

For this i get text not defined 为此,我得到未定义的文本

I assume you meant " test not defined?" 我假设您的意思是“ 测试未定义?” Your script only requires the mongodb library, and I don't believe test is a core nodejs function, so that would explain the error. 您的脚本仅需要mongodb库,我不认为test是核心的nodejs函数,因此可以解释该错误。

To reference the driver documentation for db.collection() , an assert library is used, but also properly imported (as you did in your second example). 为了引用db.collection()的驱动程序文档,使用了一个assert库,但也已将其正确导入(就像您在第二个示例中所做的那样)。

Within your callback to db.open() , you don't check if an error occurred. db.open()回调中,您无需检查是否发生错误。 That might shed some light on why db is null in that function. 这也许可以说明为什么该函数中的db为null。

Regarding your question about the equivalent of SQL injection with MongoDB, the main areas of concern are places where you might pass untrusted input into evaluated JavaScript, or using such input to construct free-form query objects (not simply using a string, but more like dropping an object into your BSON query). 关于与MongoDB等同于SQL注入的问题,主要关注的领域是您可能在其中将不受信任的输入传递到经过评估的JavaScript中,或者使用此类输入来构造自由格式的查询对象(不仅使用字符串,而且更像将对象放入您的BSON查询中)。 Both of these links should provide more information on the subject: 这两个链接都应提供有关该主题的更多信息:

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

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