简体   繁体   English

使用MEAN堆栈从Mongo返回集合

[英]Return collections from Mongo using a MEAN stack

I'm currently working on a group project. 我目前正在做一个小组项目。 We're using the MEAN stack to create a simple web application but are running into some issues with returning data from the mongo database. 我们正在使用MEAN堆栈创建一个简单的Web应用程序,但是在从mongo数据库返回数据时遇到了一些问题。

We're trying to return all documents in a collection stored in a mongo db (hosted at mongolab) but running into issues with retrieving the data. 我们试图返回存储在mongo db(位于mongolab中)中的集合中的所有文档,但是在检索数据时遇到了问题。

We're using the following Mongoose code within our node server to create our schema: 我们在节点服务器中使用以下Mongoose代码创建架构:

var challengeSchema = mongoose.Schema({
name: String,
description: String,
difficulty: Number,
numberOfTimesCompleted: Number,
comments: [{user: String, body: String, date: Date }]
});

var Challenge = mongoose.model('Challenge', challengeSchema);

Using this schema, a collection called "challenges" is added to the database. 使用此模式,一个名为“挑战”的集合被添加到数据库中。

While we're having no difficulties in adding documents to the collection, when we try to return all the documents from this collection, nothing returns (it times out). 尽管在添加文档到集合中没有任何困难,但是当我们尝试从该集合返回所有文档时,没有任何返回(超时)。

This is the code we're currently using to query the database: 这是我们当前用于查询数据库的代码:

exports.getChallenges = function (req, res){

    var challengeList = {};

    Challenge.find(function (err, Challenge) {
        if (err)
        challengeList = Challenge;
        console.log(Challenge);
        res.send(challengeList);
    });    
};

We were thinking the issue had something to do with the fact that the collection is called "challenges" and our model is called "Challenge." 我们认为该问题与以下事实有关:集合称为“挑战”,而我们的模型称为“挑战”。 After playing with the code for a while we weren't able to make much progress. 在玩了一段时间的代码后,我们无法取得太大的进步。

Any ideas what we might be doing wrong? 任何想法,我们可能做错了什么? All of us are new to mongo and node and feel like we are most likely making a simple mistake. 我们所有人都是mongo和node的新手,觉得我们很可能会犯一个简单的错误。 Thanks a lot! 非常感谢!

From reading the Mongoose docs, it looks like the mongo collection name will be induced from the model name, unless you supply it as the third parameter to the mongoose.model method. 通过阅读Mongoose文档,看起来mongo集合名称将从模型名称中得出,除非您将其作为mongoose.model方法的第三个参数提供。

So in your example, you might try: 因此,在您的示例中,您可以尝试:

var Challenge = mongoose.model('Challenge', challengeSchema, 'challenges');

The docs for that mongoose method are here: http://mongoosejs.com/docs/api.html#index_Mongoose-model 该猫鼬方法的文档在这里: http : //mongoosejs.com/docs/api.html#index_Mongoose-model

In addition, it looks like you're not supplying any conditions to the mongoose.find method. 此外,您似乎没有为mongoose.find方法提供任何条件。 Here's the API documentation for that: http://mongoosejs.com/docs/api.html#model_Model.find . 这是该API的文档: http : //mongoosejs.com/docs/api.html#model_Model.find So you'd want something like: 因此,您需要类似:

Challenge.find({find_my_criteria:'here'},function (err, challenges) {
    res.send(challenges);
});

...obviously you would want to substitute your actual conditions/criteria with something from the req , as necessary. ……显然,您需要根据需要用req代替您的实际条件/标准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM