如何通过mongojs模块从nodejs查询mongodb? (服务器是openshift)

时间:2022-06-01 22:05:11

I was trying out mongodb and nodejs on openshift, using mongojs to interface between nodejs and mongodb.

我在openshift上尝试使用mongodb和nodejs,使用mongojs在nodejs和mongodb之间进行接口。

In mongoshell I ran "use nodejs" and defined a "scores" collection. I saved some data in it and its correctly showing.

在mongoshell中,我运行了“use nodejs”并定义了一个“得分”集合。我在其中保存了一些数据并正确显示。

In app.js file of nodeserver I have

在nodeserver的app.js文件中我有

self.routes['/db'] = function(req, res) {
var db = require("./db");
        db.scores.find(function(err,docs){res.send(docs);});        
    };

and in db.js file I have

在db.js文件中我有

var dbName = "/nodejs";
var databaseUrl = process.env.OPENSHIFT_MONGODB_DB_USERNAME + ":" +  process.env.OPENSHIFT_MONGODB_DB_PASSWORD + "@" + process.env.OPENSHIFT_MONGODB_DB_HOST + ":" +  process.env.OPENSHIFT_MONGODB_DB_PORT+ dbName;
// "username:password@example.com/mydb"
var collections = ["scores"]
var db = require("mongojs").connect(databaseUrl, collections);
module.exports = db;

I am unable to get any data when I go to url mydomain.com/db

当我去url mydomain.com/db时,我无法获得任何数据

Can someone please point out what am doing wrong. The database is connecting. I am unable to find all from scores collection.

有人可以指出我做错了什么。数据库正在连接。我无法从乐谱收藏中找到所有内容。

self.routes['/db'] = function(req, res) {
    var db = require("./db");
    db.scores.find(function(err,docs){res.send(docs);});        
};

4 个解决方案

#1


1  

I think that you should use the collection method to use your score collection. Like the following:

我认为您应该使用收集方法来使用您的乐谱集合。如下:

db.collection('scores').find(function(err,docs){res.send(docs);});

Or use the toArray function to be sure to retrieve an array of objects.

或者使用toArray函数确保检索对象数组。

db.collection('scores').find().toArray(function(err, docs) {
    console.dir(docs);
    res.send(docs)
}

#2


1  

this is how we connect to our MongoDB server:

这是我们连接到MongoDB服务器的方式:

//app.js
var databaseUrl = "mydb"; // "username:password@example.com/mydb"
var collections = ["users"]
var db = require("mongojs").connect(databaseUrl, collections);



 db.users.find({email: "abcd@gmail.com"}, function(err, users) {
  if( err || !users) console.log("No female users found");
  else users.forEach( function(femaleUser) {
    console.log(femaleUser);
  } );
});

#3


0  

The db connection configuration I saved in a separate file was the one causing error. This configuration worked and I was able to fetch db entries. Use either the commented out block OR the code below that. Both does the same thing. Just posting this answer for anyone trying out on openshift.

我保存在单独文件中的数据库连接配置是导致错误的配置。这个配置工作,我能够获取数据库条目。使用注释掉的块或下面的代码。两者都做同样的事情。只需为尝试使用openshift的人发布此答案。

self.routes['/db'] = function(req, res) {
        var dbName = "/nodejs";
        var connection_string = process.env.OPENSHIFT_MONGODB_DB_USERNAME + ":" +  process.env.OPENSHIFT_MONGODB_DB_PASSWORD + "@" + process.env.OPENSHIFT_MONGODB_DB_HOST + ":" +  process.env.OPENSHIFT_MONGODB_DB_PORT + dbName;
        var db = mongojs(connection_string);

        /*
        var b = db.collection('books');    
        db.b.find(function(err, docs) {
           res.send(docs); 
        });*/
        db.collection('books').find(function(err,docs){
            res.send(docs);
        });
    };

nodejs is the database name used in which the collection "books" were stored. Defined var mongojs=require('mongojs'); at the very top where all variables were declared, though it doesn't matter if its just declared before mongojs variable is used.

nodejs是用于存储集合“books”的数据库名称。定义var mongojs = require('mongojs');在声明所有变量的最顶层,尽管如果在使用mongojs变量之前声明它是无关紧要的。

#4


0  

Using MongoClient.

//test is the database and students is the collection inside test.
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    console.log("Successfully connected to MongoDB ");
    var cursor = db.collection('students').find({});

    cursor.forEach(
        function(doc) {
           console.log("JSON doc: "+doc);
        },
        function(err) {
            console.log("Error");
            return db.close();
        }
    );

});

#1


1  

I think that you should use the collection method to use your score collection. Like the following:

我认为您应该使用收集方法来使用您的乐谱集合。如下:

db.collection('scores').find(function(err,docs){res.send(docs);});

Or use the toArray function to be sure to retrieve an array of objects.

或者使用toArray函数确保检索对象数组。

db.collection('scores').find().toArray(function(err, docs) {
    console.dir(docs);
    res.send(docs)
}

#2


1  

this is how we connect to our MongoDB server:

这是我们连接到MongoDB服务器的方式:

//app.js
var databaseUrl = "mydb"; // "username:password@example.com/mydb"
var collections = ["users"]
var db = require("mongojs").connect(databaseUrl, collections);



 db.users.find({email: "abcd@gmail.com"}, function(err, users) {
  if( err || !users) console.log("No female users found");
  else users.forEach( function(femaleUser) {
    console.log(femaleUser);
  } );
});

#3


0  

The db connection configuration I saved in a separate file was the one causing error. This configuration worked and I was able to fetch db entries. Use either the commented out block OR the code below that. Both does the same thing. Just posting this answer for anyone trying out on openshift.

我保存在单独文件中的数据库连接配置是导致错误的配置。这个配置工作,我能够获取数据库条目。使用注释掉的块或下面的代码。两者都做同样的事情。只需为尝试使用openshift的人发布此答案。

self.routes['/db'] = function(req, res) {
        var dbName = "/nodejs";
        var connection_string = process.env.OPENSHIFT_MONGODB_DB_USERNAME + ":" +  process.env.OPENSHIFT_MONGODB_DB_PASSWORD + "@" + process.env.OPENSHIFT_MONGODB_DB_HOST + ":" +  process.env.OPENSHIFT_MONGODB_DB_PORT + dbName;
        var db = mongojs(connection_string);

        /*
        var b = db.collection('books');    
        db.b.find(function(err, docs) {
           res.send(docs); 
        });*/
        db.collection('books').find(function(err,docs){
            res.send(docs);
        });
    };

nodejs is the database name used in which the collection "books" were stored. Defined var mongojs=require('mongojs'); at the very top where all variables were declared, though it doesn't matter if its just declared before mongojs variable is used.

nodejs是用于存储集合“books”的数据库名称。定义var mongojs = require('mongojs');在声明所有变量的最顶层,尽管如果在使用mongojs变量之前声明它是无关紧要的。

#4


0  

Using MongoClient.

//test is the database and students is the collection inside test.
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    console.log("Successfully connected to MongoDB ");
    var cursor = db.collection('students').find({});

    cursor.forEach(
        function(doc) {
           console.log("JSON doc: "+doc);
        },
        function(err) {
            console.log("Error");
            return db.close();
        }
    );

});