0

我正在尝试在 MongoDB 中保存/插入

使用 express 和 mongoJS

我的代码:

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

当我去 Mongo-Console 以“显示数据库”时,它将显示新创建的数据库

当我在控制台“db.hexx.users.find()”中使用它时它是空的,当我插入每个控制台“db.hexx.users.insert({email: "srirangan@gmail.com", password: "iLoveddMonfxxgodd" , sex: "male"})" - 它插入

在 nodeJs 中它不起作用

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

谢谢

4

2 回答 2

0

我认为你控制台命令错了,

尝试

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

确保选择正确的集合

show collections;
于 2014-06-15T17:32:56.850 回答
0

文档似乎有点偏离和误导,但这对我有用:

var mongojs = require("mongojs");

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

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

然后数据将出现在集合中。

因此,似乎集合的访问器并不是直接从db变量中隐含的,您实际上需要直接获取集合。

于 2014-06-12T11:27:06.510 回答