mongoose, find data based on requestFind mongodb (in mongoose+node.JS) records based on a conditionHow do I update/upsert a document in Mongoose?Find the version of an installed npm packageDynamically create collection with MongooseBest way to perform a full text search in MongoDB and MongooseFind and Count elements of collection with Mongoosemongoose finding records with multiple conditions and checking if the request parameter not emptyMongoose find document by key whose value is a complex objectMongoose find() with multiple criteriaMongoose - find() - based on variable

In what sequence should an advanced civilization teach technology to medieval society to maximize rate of adoption?

Wrong Schengen Visa exit stamp on my passport, who can I complain to?

Can I include Abandoned Patent in CV?

Does a large scratch in an ND filter affect image quality?

What does "boys rule, girls drool" mean?

Finding partition with maximum number of edges between sets

Why is the car dealer insisting on a loan instead of cash?

Nurikabe minicubes: the Headache, the Panache, the Apache

Importance of the current postdoc advisor's letter in TT job search

Why is the year in this ISO timestamp not 2019?

Why is belonging not transitive?

Pronunciation of "солнце"

How to write characters doing illogical things in a believable way?

Unable to find solution to 6 simultaneous equations

What does the Free Recovery sign (UK) actually mean?

How to publish superseding results without creating enemies

Good notation to require that z ≠ 0, -1, -2, -3, ...

Has Dumbledore ever scolded Harry?

Why is the UK still pressing on with Brexit?

Is the Dodge action perceptible to other characters?

Python web-scraper to download table of transistor counts from Wikipedia

A command to output each line forward then backwards

Help with wheel lock

What do these two notes together mean?



mongoose, find data based on request


Find mongodb (in mongoose+node.JS) records based on a conditionHow do I update/upsert a document in Mongoose?Find the version of an installed npm packageDynamically create collection with MongooseBest way to perform a full text search in MongoDB and MongooseFind and Count elements of collection with Mongoosemongoose finding records with multiple conditions and checking if the request parameter not emptyMongoose find document by key whose value is a complex objectMongoose find() with multiple criteriaMongoose - find() - based on variable






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I want to customize my search crateria on mongoose collection based on request parameter



Course.find(

instTypeId: 1,
status: 1,
"institute.instId": req.body.instId,
"institute.majorId": req.body.majorId
,
)


I tried to do that but if the req.intId or req.major id is empty it only get the records that have empty institute.instId



In case the req.intId or req.major or both I dont want to include them in the find










share|improve this question


























  • What kind of request data are you expecting? from query parameter or body? or is req something else?

    – 1565986223
    Mar 28 at 12:35











  • req = request.body, I am expecting that the two values would be empty or one of them is not or both are not empty

    – MuhIbra
    Mar 28 at 12:48


















0















I want to customize my search crateria on mongoose collection based on request parameter



Course.find(

instTypeId: 1,
status: 1,
"institute.instId": req.body.instId,
"institute.majorId": req.body.majorId
,
)


I tried to do that but if the req.intId or req.major id is empty it only get the records that have empty institute.instId



In case the req.intId or req.major or both I dont want to include them in the find










share|improve this question


























  • What kind of request data are you expecting? from query parameter or body? or is req something else?

    – 1565986223
    Mar 28 at 12:35











  • req = request.body, I am expecting that the two values would be empty or one of them is not or both are not empty

    – MuhIbra
    Mar 28 at 12:48














0












0








0


0






I want to customize my search crateria on mongoose collection based on request parameter



Course.find(

instTypeId: 1,
status: 1,
"institute.instId": req.body.instId,
"institute.majorId": req.body.majorId
,
)


I tried to do that but if the req.intId or req.major id is empty it only get the records that have empty institute.instId



In case the req.intId or req.major or both I dont want to include them in the find










share|improve this question
















I want to customize my search crateria on mongoose collection based on request parameter



Course.find(

instTypeId: 1,
status: 1,
"institute.instId": req.body.instId,
"institute.majorId": req.body.majorId
,
)


I tried to do that but if the req.intId or req.major id is empty it only get the records that have empty institute.instId



In case the req.intId or req.major or both I dont want to include them in the find







node.js mongoose






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 12:48







MuhIbra

















asked Mar 28 at 12:30









MuhIbraMuhIbra

12 bronze badges




12 bronze badges















  • What kind of request data are you expecting? from query parameter or body? or is req something else?

    – 1565986223
    Mar 28 at 12:35











  • req = request.body, I am expecting that the two values would be empty or one of them is not or both are not empty

    – MuhIbra
    Mar 28 at 12:48


















  • What kind of request data are you expecting? from query parameter or body? or is req something else?

    – 1565986223
    Mar 28 at 12:35











  • req = request.body, I am expecting that the two values would be empty or one of them is not or both are not empty

    – MuhIbra
    Mar 28 at 12:48

















What kind of request data are you expecting? from query parameter or body? or is req something else?

– 1565986223
Mar 28 at 12:35





What kind of request data are you expecting? from query parameter or body? or is req something else?

– 1565986223
Mar 28 at 12:35













req = request.body, I am expecting that the two values would be empty or one of them is not or both are not empty

– MuhIbra
Mar 28 at 12:48






req = request.body, I am expecting that the two values would be empty or one of them is not or both are not empty

– MuhIbra
Mar 28 at 12:48













3 Answers
3






active

oldest

votes


















0
















You can use Object.assign to add values to the search object if they exist:



let instId;
let majorId = 1;

if (instId)
search = Object.assign(search, "institute.instId": instId);

if (majorId)
search = Object.assign(search, "institute.majorId": majorId);


Course.find(search , (err, foundCourses) =>
// Do stuff
);





share|improve this answer
































    0
















    Assuming that you're receiving parsed req.body, quick and dirty way could be:



    Course.find()


    You can build query object in similar fashion:



    var query = 
    if (req.body.instId)
    // using object destructuring
    query = ..., "institute.instId": req.body.instId



    However, it is advisable to set up some validation depending on use case






    share|improve this answer
































      0
















      let search = 
      instTypeId: 1,
      status: 1
      ;
      if (req.body.instId)
      search["institute.instId"] = req.body.instId;

      if (req.body.majorId)
      search["institute.majorId"] = req.body.majorId;

      Course.find(search)


      I did that in order to customize the search columns






      share|improve this answer





























        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );














        draft saved

        draft discarded
















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55397704%2fmongoose-find-data-based-on-request%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0
















        You can use Object.assign to add values to the search object if they exist:



        let instId;
        let majorId = 1;

        if (instId)
        search = Object.assign(search, "institute.instId": instId);

        if (majorId)
        search = Object.assign(search, "institute.majorId": majorId);


        Course.find(search , (err, foundCourses) =>
        // Do stuff
        );





        share|improve this answer





























          0
















          You can use Object.assign to add values to the search object if they exist:



          let instId;
          let majorId = 1;

          if (instId)
          search = Object.assign(search, "institute.instId": instId);

          if (majorId)
          search = Object.assign(search, "institute.majorId": majorId);


          Course.find(search , (err, foundCourses) =>
          // Do stuff
          );





          share|improve this answer



























            0














            0










            0









            You can use Object.assign to add values to the search object if they exist:



            let instId;
            let majorId = 1;

            if (instId)
            search = Object.assign(search, "institute.instId": instId);

            if (majorId)
            search = Object.assign(search, "institute.majorId": majorId);


            Course.find(search , (err, foundCourses) =>
            // Do stuff
            );





            share|improve this answer













            You can use Object.assign to add values to the search object if they exist:



            let instId;
            let majorId = 1;

            if (instId)
            search = Object.assign(search, "institute.instId": instId);

            if (majorId)
            search = Object.assign(search, "institute.majorId": majorId);


            Course.find(search , (err, foundCourses) =>
            // Do stuff
            );






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 28 at 12:58









            bendataclearbendataclear

            3,1053 gold badges21 silver badges41 bronze badges




            3,1053 gold badges21 silver badges41 bronze badges


























                0
















                Assuming that you're receiving parsed req.body, quick and dirty way could be:



                Course.find()


                You can build query object in similar fashion:



                var query = 
                if (req.body.instId)
                // using object destructuring
                query = ..., "institute.instId": req.body.instId



                However, it is advisable to set up some validation depending on use case






                share|improve this answer





























                  0
















                  Assuming that you're receiving parsed req.body, quick and dirty way could be:



                  Course.find()


                  You can build query object in similar fashion:



                  var query = 
                  if (req.body.instId)
                  // using object destructuring
                  query = ..., "institute.instId": req.body.instId



                  However, it is advisable to set up some validation depending on use case






                  share|improve this answer



























                    0














                    0










                    0









                    Assuming that you're receiving parsed req.body, quick and dirty way could be:



                    Course.find()


                    You can build query object in similar fashion:



                    var query = 
                    if (req.body.instId)
                    // using object destructuring
                    query = ..., "institute.instId": req.body.instId



                    However, it is advisable to set up some validation depending on use case






                    share|improve this answer













                    Assuming that you're receiving parsed req.body, quick and dirty way could be:



                    Course.find()


                    You can build query object in similar fashion:



                    var query = 
                    if (req.body.instId)
                    // using object destructuring
                    query = ..., "institute.instId": req.body.instId



                    However, it is advisable to set up some validation depending on use case







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 28 at 14:24









                    15659862231565986223

                    4,1012 gold badges4 silver badges21 bronze badges




                    4,1012 gold badges4 silver badges21 bronze badges
























                        0
















                        let search = 
                        instTypeId: 1,
                        status: 1
                        ;
                        if (req.body.instId)
                        search["institute.instId"] = req.body.instId;

                        if (req.body.majorId)
                        search["institute.majorId"] = req.body.majorId;

                        Course.find(search)


                        I did that in order to customize the search columns






                        share|improve this answer































                          0
















                          let search = 
                          instTypeId: 1,
                          status: 1
                          ;
                          if (req.body.instId)
                          search["institute.instId"] = req.body.instId;

                          if (req.body.majorId)
                          search["institute.majorId"] = req.body.majorId;

                          Course.find(search)


                          I did that in order to customize the search columns






                          share|improve this answer





























                            0














                            0










                            0









                            let search = 
                            instTypeId: 1,
                            status: 1
                            ;
                            if (req.body.instId)
                            search["institute.instId"] = req.body.instId;

                            if (req.body.majorId)
                            search["institute.majorId"] = req.body.majorId;

                            Course.find(search)


                            I did that in order to customize the search columns






                            share|improve this answer















                            let search = 
                            instTypeId: 1,
                            status: 1
                            ;
                            if (req.body.instId)
                            search["institute.instId"] = req.body.instId;

                            if (req.body.majorId)
                            search["institute.majorId"] = req.body.majorId;

                            Course.find(search)


                            I did that in order to customize the search columns







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Mar 30 at 15:19

























                            answered Mar 30 at 11:18









                            MuhIbraMuhIbra

                            12 bronze badges




                            12 bronze badges































                                draft saved

                                draft discarded















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55397704%2fmongoose-find-data-based-on-request%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript