我应该在Mongoose中一个接一个地返回数组或数据吗 [英] Should I return an array or data one by one in Mongoose

查看:246
本文介绍了我应该在Mongoose中一个接一个地返回数组或数据吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用IOS创建的简单应用程序,它是一个问卷调查应用程序,每当用户单击播放时,它将调用对node.js/express服务器的请求

从技术上讲,用户单击答案后,它将转到下一个问题

我很困惑使用哪种方法来获取问题/问题

  1. 一次获取所有数据并将其呈现给用户-这是一个数组
  2. 随着用户处理下一个问题,一个接一个地获取数据-每次通话一个数据

API示例

// Fetch all the data at once
app.get(‘/api/questions’, (req, res, next) => {
  Question.find({}, (err, questions) => {
    res.json(questions);
  });
});

// Fetch the data one by one
app.get('/api/questions/:id', (req, res, next) => {
  Question.findOne({ _id: req.params.id }, (err, question) => {
   res.json(question);
  });
});

第一种方法的问题是,假设有200个问题,mongodb一次获取不是很慢,并且可能无法进行网络请求

2号方法的问题,我简直无法想象如何做到这一点,因为每个问题都是独立的,并且触发下一个api调用只是很奇怪,除非问题mongodb中有一个计数器或一个级别.

为了清楚起见,这是猫鼬中的问题数据库设计

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const QuestionSchema = new Schema({
    question: String,
    choice_1: String,
    choice_2: String,
    choice_3: String,
    choice_4: String,
    answer: String
});

解决方案

很好的问题.我想这个问题的答案取决于您对这个应用程序的未来计划.

如果您计划有500个问题,那么一一回答将需要500个api调用.并非总是最好的选择.另一方面,如果一次获取所有这些对象,则将延迟响应,具体取决于每个对象的大小.

所以我的建议是使用分页.带来10个结果,当用户达到第8个问题时,用接下来的10个问题更新列表.

这是移动开发人员的常见做法,这也使您可以灵活地根据用户先前的回答来更新下一个问题.就像自适应测试一样.

编辑

您可以添加pageNumber&您请求从服务器获取问题的请求中的pageSize查询参数,类似这样.

myquestionbank.com/api/questions?pageNumber=10&pageSize=2

在服务器上接收这些参数

var pageOptions = {
    pageNumber: req.query.pageNumber || 0,
    pageSize: req.query.pageSize || 10
}

并在从数据库查询时提供这些附加参数.

Question.find()
    .skip(pageOptions.pageNumber * pageOptions.pageSize)
    .limit(pageOptions.pageOptions)
    .exec(function (err, questions) {
        if(err) {
        res.status(500).json(err); return; 
        };

        res.status(200).json(questions);
    })

注意:您的pageNumber开头为零(0),这不是强制性的,但这是约定.

skip() 方法可让您跳过前n个结果.考虑第一种情况,pageNumber将为零,因此乘积(pageOptions.pageNumber * pageOptions.pageSize)将变为零,并且不会跳过任何记录. 但是对于下一次(pageNumber = 1),乘积结果将为10.因此,它将跳过前10个已经处理过的结果.

limit() 此方法限制了将在结果中提供的记录数.

请记住,您需要为每个请求更新pageNumber变量. (尽管您也可以更改限制,但建议在所有请求中都将其保持不变)

因此,您要做的就是,当用户遇到倒数第二个问题时,就可以将服务器放入数组中再向服务器请求10个(pageSize)个问题.

代码参考:此处. /p>

I have this simple app that I created using IOS, it is a questionnaire app, whenever user clicks play, it will invoke a request to node.js/express server

Technically after a user clicks an answer it will go to the next question

I'm confused to use which method, to fetch the questions/question

  1. fetch all the data at once and present it to the user - which is an array
  2. Fetch the data one by one as user progress with the next question - which is one data per call

API examples

// Fetch all the data at once
app.get(‘/api/questions’, (req, res, next) => {
  Question.find({}, (err, questions) => {
    res.json(questions);
  });
});

// Fetch the data one by one
app.get('/api/questions/:id', (req, res, next) => {
  Question.findOne({ _id: req.params.id }, (err, question) => {
   res.json(question);
  });
});

The problem with number 1 approach is that, let say there are 200 questions, wouldn’t it be slow for mongodb to fetch at once and possibly slow to do network request

The problem with number 2 approach, I just can’t imagine how to do this, because every question is independent and to trigger to next api call is just weird, unless there is a counter or a level in the question mongodb.

Just for the sake of clarity, this is the question database design in Mongoose

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const QuestionSchema = new Schema({
    question: String,
    choice_1: String,
    choice_2: String,
    choice_3: String,
    choice_4: String,
    answer: String
});

解决方案

Very good question. I guess the answer to this question depends on your future plans about this app.

If you are planning to have 500 questions, then getting them one by one will require 500 api calls. Not the best option always. On the other hand, if you fetch all of them at once, it will delay the response depending on the size of each object.

So my suggestion will be to use pagination. Bring 10 results, when the user reaches 8th question update the list with next 10 questions.

This is a common practice among mobile developers, this will also give you the flexibility to update next questions on the basis of previous responses from user. Like Adaptive test and all.

EDIT

You can add pageNumber & pageSize query parameter in your request for fetching questions from server, something like this.

myquestionbank.com/api/questions?pageNumber=10&pageSize=2

receive these parameters in on server

var pageOptions = {
    pageNumber: req.query.pageNumber || 0,
    pageSize: req.query.pageSize || 10
}

and while querying from your database provide these additional parameters.

Question.find()
    .skip(pageOptions.pageNumber * pageOptions.pageSize)
    .limit(pageOptions.pageOptions)
    .exec(function (err, questions) {
        if(err) {
        res.status(500).json(err); return; 
        };

        res.status(200).json(questions);
    })

Note: start your pageNumber with zero (0) it's not mandatory, but that's the convention.

skip() method allows you to skip first n results. Consider the first case, pageNumber will be zero, so the product (pageOptions.pageNumber * pageOptions.pageSize) will become zero, and it will not skip any record. But for next time (pageNumber=1) the product will result to 10. so it will skip first 10 results which were already processed.

limit() this method limits the number of records which will be provided in result.

Remember that you'll need to update pageNumber variable with each request. (though you can vary limit also, but it is advised to keep it same in all the requests)

So, all you have to do is, as soon as user reaches second last question, you can request for 10 (pageSize) more questions from the server as put it in your array.

code reference : here.

这篇关于我应该在Mongoose中一个接一个地返回数组或数据吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆