Creating Mongoose model
Clash 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.
javascript node.js database mongodb mongoose
add a comment |Â
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.
javascript node.js database mongodb mongoose
add a comment |Â
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.
javascript node.js database mongodb mongoose
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.
javascript node.js database mongodb mongoose
edited Mar 26 at 0:36
Jamalâ¦
30.1k11114225
30.1k11114225
asked Mar 20 at 15:07
abrarisme
1639
1639
add a comment |Â
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered Apr 10 at 12:02
Muhammad Faizan
2063
2063
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password