How to implement mongodb within node.js from scratch using cygwin
How would you implement an asynch mongodb 开发者_JAVA百科using vistax64/cygwin/node.js from scratch which resembles the original shell?
When I try tutorials, I get things all very similar too:
admin@RainComputer ~/nodeProjects
$ node dbtest.js
mongo://localhost:27017: Error: EPERM, Operation not permitted
Finished scanning... primary? no
undefined
mongo://localhost:27017: Disconnected
I just need any advice to install from scratch (just node and npm) a mongodb driver, preferably without using network functions, and close to the original shell.
I got this working by doing the following:
- In cygwin: Install Node and npm (node package manager) and install ExpressJS, Jade and whatever else you'd like.
- In Windows: download and install mongoDB. Start it running with "mongod" and note the port it listens on. Then test from the console using "mongo". All should work within windows before trying from Cygwin.
- In cygwin: Install a mongo driver using "npm install mongodb"
from your javascript code, connect to the DB:
var Db = require('../node_modules/mongodb/lib/mongodb').Db, Server = require('../node_modules/mongodb/lib/mongodb').Server, ObjectID= require('../node_modules/mongodb/lib/mongodb/bson/bson').ObjectID; ... this.db= new Db('my-db-name', new Server("127.0.0.1", <your port here>, {auto_reconnect: true}, {} )); this.db.open( function(err, db) { sys.puts("Error : " + err ) ; }
If you get no errors, check in the mongo console with "show dbs" you should see a new DB named "my-db-name" in there. One more thing: run cygwin "as administrator" in windows, I'm not sure if it matters but just in case.
Hope that helps, -fs
Just download and install the windows 64 bit version of MongoDB from the official website.
Run it normally on windows (outside of cygwin).
You will still be able to connect from nodejs within cygwin.
The installation of mongodb will also come with the shell application so you can run mongodb commands directly to the database.
精彩评论