0

Iam trying to save/insert in MongoDB

using express and mongoJS

My Code :

var port = 8580;
var express = require ('express');
var app = express ();


// import mongojs Modul
var mongojs = require("mongojs");


//connect
var db = mongojs.connect("localhost:27017/hexx",["users", "reports"]);


//unser Pfad
app.get('/', function(req,res){

    db.users.insert({email: "srirangan@gmail.com", password: "iLoveddMonfxxgodd", sex: "male"});



    console.log("HTTP server running at Port// :" + port);

    db.users.save({created:'just now'});

    console.log(db.users);

});


app.listen(port);

When I go to Mongo-Console to "show databases" it will show the new created Database

and when I use in console "db.hexx.users.find()" its empty and when I insert per console "db.hexx.users.insert({email: "srirangan@gmail.com", password: "iLoveddMonfxxgodd", sex: "male"})" - it inserts

In nodeJs its not working

db.users.insert({email: "srirangan@gmail.com", password: "iLoveddMonfxxgodd", sex: "male"});

Thanks

user1175380
  • 87
  • 2
  • 10

2 Answers2

0

The documentation seems a bit off and misleading but this is what works for me:

var mongojs = require("mongojs");

var db = mongojs.connect("localhost/hexx");

var users = db.collection('users');
users.insert({ email: "me@me.com", pasword: "password" });

The data will then be present in the collection.

So it seems like the accessor for collections is not implied straight from the db variable and you actually need to fetch the collection directly.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • its not working, I have copy&pasted it to server.js and runed via console – user1175380 Jun 12 '14 at 11:43
  • @user1175380 Would not have posted with "works for me" if I had not tested myself. You are doing something different. I created a new project directory, added the dependency to `packages.json` and the listing above is the content of the sole script being run. Please do that first. Then sort out other conflicts in your application. – Neil Lunn Jun 12 '14 at 11:48
0

i think you console command that wrong ,

try to

use hexx;   // from localhost:27017/hexx
db.users.find(); // db.users.insert

make sure to select collections correct with

show collections;