Creating Mongoose model

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





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







up vote
1
down vote

favorite












I'm creating a Mongoose model for a web service. I have a secondary index on facultyId because majority of the reads would want to search by facultyId. I've included some statics on the model in order to make it simple for people to query.



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

mongoose.Promise = global.Promise;

const GAPFApplicationSchema = new Schema(
facultyId: type: Number, index: true ,
created: Number, // use UNIX time stamps for all dates
lastModified: Number,
status: type: String, enum: ["SUBMITTED", "BUDGET_ALLOCATED"] ,
attachedDocuments: [

name: String,
link: String,
attachedDate: Number

]
);

GAPFApplicationSchema.static("findByFacultyId", function(facultyId, callback)
return this.findOne()
.where("facultyId")
.equals(facultyId);
);

GAPFApplicationSchema.static("submit", function(gapf, callback)
return this.findOneAndUpdate(
facultyId: gapf.facultyId ,
gapf,
upsert: true , // create it if doesn't exist
callback
);
);

const GAPFApplication = mongoose.model(
"GAPFApplication",
GAPFApplicationSchema
);

export GAPFApplication ;


I haven't used Mongoose before, so I was hoping to get some review on the approach taken here. The code is pretty short, so I didn't think there would be much to address.







share|improve this question



























    up vote
    1
    down vote

    favorite












    I'm creating a Mongoose model for a web service. I have a secondary index on facultyId because majority of the reads would want to search by facultyId. I've included some statics on the model in order to make it simple for people to query.



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

    mongoose.Promise = global.Promise;

    const GAPFApplicationSchema = new Schema(
    facultyId: type: Number, index: true ,
    created: Number, // use UNIX time stamps for all dates
    lastModified: Number,
    status: type: String, enum: ["SUBMITTED", "BUDGET_ALLOCATED"] ,
    attachedDocuments: [

    name: String,
    link: String,
    attachedDate: Number

    ]
    );

    GAPFApplicationSchema.static("findByFacultyId", function(facultyId, callback)
    return this.findOne()
    .where("facultyId")
    .equals(facultyId);
    );

    GAPFApplicationSchema.static("submit", function(gapf, callback)
    return this.findOneAndUpdate(
    facultyId: gapf.facultyId ,
    gapf,
    upsert: true , // create it if doesn't exist
    callback
    );
    );

    const GAPFApplication = mongoose.model(
    "GAPFApplication",
    GAPFApplicationSchema
    );

    export GAPFApplication ;


    I haven't used Mongoose before, so I was hoping to get some review on the approach taken here. The code is pretty short, so I didn't think there would be much to address.







    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm creating a Mongoose model for a web service. I have a secondary index on facultyId because majority of the reads would want to search by facultyId. I've included some statics on the model in order to make it simple for people to query.



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

      mongoose.Promise = global.Promise;

      const GAPFApplicationSchema = new Schema(
      facultyId: type: Number, index: true ,
      created: Number, // use UNIX time stamps for all dates
      lastModified: Number,
      status: type: String, enum: ["SUBMITTED", "BUDGET_ALLOCATED"] ,
      attachedDocuments: [

      name: String,
      link: String,
      attachedDate: Number

      ]
      );

      GAPFApplicationSchema.static("findByFacultyId", function(facultyId, callback)
      return this.findOne()
      .where("facultyId")
      .equals(facultyId);
      );

      GAPFApplicationSchema.static("submit", function(gapf, callback)
      return this.findOneAndUpdate(
      facultyId: gapf.facultyId ,
      gapf,
      upsert: true , // create it if doesn't exist
      callback
      );
      );

      const GAPFApplication = mongoose.model(
      "GAPFApplication",
      GAPFApplicationSchema
      );

      export GAPFApplication ;


      I haven't used Mongoose before, so I was hoping to get some review on the approach taken here. The code is pretty short, so I didn't think there would be much to address.







      share|improve this question













      I'm creating a Mongoose model for a web service. I have a secondary index on facultyId because majority of the reads would want to search by facultyId. I've included some statics on the model in order to make it simple for people to query.



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

      mongoose.Promise = global.Promise;

      const GAPFApplicationSchema = new Schema(
      facultyId: type: Number, index: true ,
      created: Number, // use UNIX time stamps for all dates
      lastModified: Number,
      status: type: String, enum: ["SUBMITTED", "BUDGET_ALLOCATED"] ,
      attachedDocuments: [

      name: String,
      link: String,
      attachedDate: Number

      ]
      );

      GAPFApplicationSchema.static("findByFacultyId", function(facultyId, callback)
      return this.findOne()
      .where("facultyId")
      .equals(facultyId);
      );

      GAPFApplicationSchema.static("submit", function(gapf, callback)
      return this.findOneAndUpdate(
      facultyId: gapf.facultyId ,
      gapf,
      upsert: true , // create it if doesn't exist
      callback
      );
      );

      const GAPFApplication = mongoose.model(
      "GAPFApplication",
      GAPFApplicationSchema
      );

      export GAPFApplication ;


      I haven't used Mongoose before, so I was hoping to get some review on the approach taken here. The code is pretty short, so I didn't think there would be much to address.









      share|improve this question












      share|improve this question




      share|improve this question








      edited Mar 26 at 0:36









      Jamal♦

      30.1k11114225




      30.1k11114225









      asked Mar 20 at 15:07









      abrarisme

      1639




      1639




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          You have created a good model overall and I can only suggest you some consideration.



          Instead of using, created & lastModified.
          I would recommend you to use timestamp option in mongoose.



          const GAPFApplicationSchema = new Schema(
          facultyId: type: Number, index: true ,
          status: type: String, enum: ["SUBMITTED", "BUDGET_ALLOCATED"] ,
          attachedDocuments: [

          name: String,
          link: String,
          attachedDate: Number

          ]
          , timestamp: true);


          With this you won't need lastModified & created you will get 2 attributes createdAt & updatedAt and they would be handled by mongoose, so you wont have to worry about updating them.



          status: type: String, enum: ["SUBMITTED", "BUDGET_ALLOCATED"] ,


          you can also improve this piece of code, by changing it to number and create an internal mapping, this would allow mongoDB to search faster, as comparing string is much heavier task, than comparing numbers.






          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            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: "196"
            ;
            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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190038%2fcreating-mongoose-model%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            You have created a good model overall and I can only suggest you some consideration.



            Instead of using, created & lastModified.
            I would recommend you to use timestamp option in mongoose.



            const GAPFApplicationSchema = new Schema(
            facultyId: type: Number, index: true ,
            status: type: String, enum: ["SUBMITTED", "BUDGET_ALLOCATED"] ,
            attachedDocuments: [

            name: String,
            link: String,
            attachedDate: Number

            ]
            , timestamp: true);


            With this you won't need lastModified & created you will get 2 attributes createdAt & updatedAt and they would be handled by mongoose, so you wont have to worry about updating them.



            status: type: String, enum: ["SUBMITTED", "BUDGET_ALLOCATED"] ,


            you can also improve this piece of code, by changing it to number and create an internal mapping, this would allow mongoDB to search faster, as comparing string is much heavier task, than comparing numbers.






            share|improve this answer

























              up vote
              1
              down vote













              You have created a good model overall and I can only suggest you some consideration.



              Instead of using, created & lastModified.
              I would recommend you to use timestamp option in mongoose.



              const GAPFApplicationSchema = new Schema(
              facultyId: type: Number, index: true ,
              status: type: String, enum: ["SUBMITTED", "BUDGET_ALLOCATED"] ,
              attachedDocuments: [

              name: String,
              link: String,
              attachedDate: Number

              ]
              , timestamp: true);


              With this you won't need lastModified & created you will get 2 attributes createdAt & updatedAt and they would be handled by mongoose, so you wont have to worry about updating them.



              status: type: String, enum: ["SUBMITTED", "BUDGET_ALLOCATED"] ,


              you can also improve this piece of code, by changing it to number and create an internal mapping, this would allow mongoDB to search faster, as comparing string is much heavier task, than comparing numbers.






              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                You have created a good model overall and I can only suggest you some consideration.



                Instead of using, created & lastModified.
                I would recommend you to use timestamp option in mongoose.



                const GAPFApplicationSchema = new Schema(
                facultyId: type: Number, index: true ,
                status: type: String, enum: ["SUBMITTED", "BUDGET_ALLOCATED"] ,
                attachedDocuments: [

                name: String,
                link: String,
                attachedDate: Number

                ]
                , timestamp: true);


                With this you won't need lastModified & created you will get 2 attributes createdAt & updatedAt and they would be handled by mongoose, so you wont have to worry about updating them.



                status: type: String, enum: ["SUBMITTED", "BUDGET_ALLOCATED"] ,


                you can also improve this piece of code, by changing it to number and create an internal mapping, this would allow mongoDB to search faster, as comparing string is much heavier task, than comparing numbers.






                share|improve this answer













                You have created a good model overall and I can only suggest you some consideration.



                Instead of using, created & lastModified.
                I would recommend you to use timestamp option in mongoose.



                const GAPFApplicationSchema = new Schema(
                facultyId: type: Number, index: true ,
                status: type: String, enum: ["SUBMITTED", "BUDGET_ALLOCATED"] ,
                attachedDocuments: [

                name: String,
                link: String,
                attachedDate: Number

                ]
                , timestamp: true);


                With this you won't need lastModified & created you will get 2 attributes createdAt & updatedAt and they would be handled by mongoose, so you wont have to worry about updating them.



                status: type: String, enum: ["SUBMITTED", "BUDGET_ALLOCATED"] ,


                you can also improve this piece of code, by changing it to number and create an internal mapping, this would allow mongoDB to search faster, as comparing string is much heavier task, than comparing numbers.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Apr 10 at 12:02









                Muhammad Faizan

                2063




                2063






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190038%2fcreating-mongoose-model%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Greedy Best First Search implementation in Rust

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    C++11 CLH Lock Implementation