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

                                SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                                은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현