2015-07-21 50 views

回答

0

看看這裏,

https://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html

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

var mongoClient = new MongoClient(new Server('localhost', 27017)); 
mongoClient.open(function(err, mongoClient) { 
    var db1 = mongoClient.db("mydb"); 

mongoClient.close(); 
}); 
+0

嗨穆昆,當我嘗試上面的代碼,它拋出一個錯誤,說它無法找到monodb模塊。錯誤:module.js:338 throw err; ^ 錯誤:在Function.Module._resolveFilename找不到模塊 '的mongodb' (module.js:336:15) 在Function.Module._load(module.js:278:25) 在Module.require(模塊.js:365:17) at require(module.js:384:17) at Object。 (module.js:478:10)上​​的(/home/hadoop/Desktop/testing.js:1:81) at Module._compile(module.js:460:26) at Object.Module._extensions..js –

+0

你是否開始使用mongodb?即轉到terminal/cmd並鍵入mongod,然後重試 – Mukund

0

爲了連接到數據庫,您需要將您連接應用程序到服務器(不知道應用程序是否可以直接連接到數據庫,但從我學到的東西來看,似乎需要服務器端語言)。因此,如果您只想使用javascript連接到數據庫,則可以考慮使用node.js + express連接到mongoDb。 I think this link is quite useful. I also learnt from this website.這個網站教你如何從頭開始,指示可以很容易地遵循。如果你更喜歡使用PHP作爲服務器端,I just googled this website which might help.在我看來,PHP更容易學習,但如果你只需要基本的CRUD操作,提到的網站就足夠了。

相關問題