Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Efficient Searching issue #2

Open
Pankajtanwarbanna opened this issue Oct 10, 2018 · 0 comments
Open

Efficient Searching issue #2

Pankajtanwarbanna opened this issue Oct 10, 2018 · 0 comments
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@Pankajtanwarbanna
Copy link
Owner

Pankajtanwarbanna commented Oct 10, 2018

Hey ,

I was writing API for finding all answered question by user who is logged in.

QuestionAnswerSchema -

var questionSchema = new mongoose.Schema({ question : { type : String, unique : true, required : true }, answers : [{ answer : { type: String, }, upvote : { type : Number }, downvote : { type : Number }, views : { type : Number }, author : { type : String }, comments : [{ comment : { type : String }, author : { type : String } }] }], author : { type : String, required: true }, tag : { type : String, required : true, default : 'Other' }, totalviews : { type : Number, default: 0 }, totallikes : { type : Number, default : 0 } });

Here if a user answers a question his username is saved in answers -> author in questionSchema. Now if i want to find out all answer by a user i have to run two loops as below -

` for(var i=0;i < questions.length;i++) {

                    if(questions[i].answers.length > 0) {

                        for(var j=0;j<questions[i].answers.length;j++) {
                            if(questions[i].answers[j].author === req.params.username) {
                                answeredArrayObj.question = questions[i].question;
                                answeredArrayObj.answer = questions[i].answers[j].answer;
                                answeredArrayObj.tag = questions[i].tag;
                                answeredArrayObj._id = questions[i]._id;

                                answeredArray.push(answeredArrayObj);

                                // again make it empty for next answer
                                answeredArrayObj = {};
                            }
                        }
                    }
                }`

It is too much time consuming.

How to contribute -

  1. Fork repo.
  2. Play with code and make suitable changes
  3. Make a PR
@Pankajtanwarbanna Pankajtanwarbanna added help wanted Extra attention is needed good first issue Good for newcomers labels Oct 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant