簡體   English   中英

Node.js和mongodb訪問mongodb

[英]Node.js and mongodb access mongodb

我正在嘗試使用node.js在Windows 8上設置mongodb,有人知道為什么我會收到此錯誤。 C:\\ users \\ phill \\ node_modules \\ mongodb \\ lib \\ mongodb \\ mongo_client.js:359也說在collection = db collection時,不能調用null的方法“ collection”。 我很難設置它。 我的目標是能夠添加到mongo db,並看到我添加或提取了我添加的內容,但是現在添加一些東西對我來說已經足夠了。 我正在嘗試我能找到的所有東西,甚至直接從網站上,也嘗試了我在這里看到的所有東西。 認為這可能是我設置事情的方式。 我的node.js保存在我的c:驅動器中,有一個文件說,其中的程序文​​件(86x)有node_modules,npm等。 路徑最終是,計算機> Windows(C :)>程序文件(86x)> nodejs。 我的Mongodb保存在我的C:驅動器上,路徑最終是Windows(C :)> mongodb-win32-x86_64-2008plus-2.4.8。 在我的C語言中:我還創建了一個文件數據,並在其中創建了另一個數據庫。 我被告知我應該只使用貓鼬,我只是在學習,所以我願意接受任何建議,鏈接或任何有幫助的東西。 我還有最后一個問題,我學習了php,然后發現了有關sql注入和類似的東西,我完全沒有看到任何有關安全性的信息,我是否也希望得到同樣的結果。 為此,我得到未定義的文本,但是我所做的一切都出現錯誤,最好的解決方法是卡在正確的關注屏幕上。

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();
});
});

也嘗試過此操作並收到錯誤,C:\\ users \\ phill \\ node_modules \\ mongodb \\ lib \\ mongodb \\ mongo_client.js:359 .... at collection = db collection,無法調用以下方法的“ collection”空值。 我在命令提示符節點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);
});

在第一個代碼示例中,您說:

為此,我得到未定義的文本

我假設您的意思是“ 測試未定義?” 您的腳本僅需要mongodb庫,我不認為test是核心的nodejs函數,因此可以解釋該錯誤。

為了引用db.collection()的驅動程序文檔,使用了一個assert庫,但也已將其正確導入(就像您在第二個示例中所做的那樣)。

db.open()回調中,您無需檢查是否發生錯誤。 這也許可以說明為什么該函數中的db為null。

關於與MongoDB等同於SQL注入的問題,主要關注的領域是您可能在其中將不受信任的輸入傳遞到經過評估的JavaScript中,或者使用此類輸入來構造自由格式的查詢對象(不僅使用字符串,而且更像將對象放入您的BSON查詢中)。 這兩個鏈接都應提供有關該主題的更多信息:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM