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;
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
add a comment
|
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
What kind ofrequest
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
add a comment
|
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
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
node.js mongoose
edited Mar 28 at 12:48
MuhIbra
asked Mar 28 at 12:30
MuhIbraMuhIbra
12 bronze badges
12 bronze badges
What kind ofrequest
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
add a comment
|
What kind ofrequest
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
add a comment
|
3 Answers
3
active
oldest
votes
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
);
add a comment
|
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
add a comment
|
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
add a comment
|
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
);
);
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
Required, but never shown
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
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
);
add a comment
|
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
);
add a comment
|
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
);
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
);
answered Mar 28 at 12:58
bendataclearbendataclear
3,1053 gold badges21 silver badges41 bronze badges
3,1053 gold badges21 silver badges41 bronze badges
add a comment
|
add a comment
|
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
add a comment
|
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
add a comment
|
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
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
answered Mar 28 at 14:24
15659862231565986223
4,1012 gold badges4 silver badges21 bronze badges
4,1012 gold badges4 silver badges21 bronze badges
add a comment
|
add a comment
|
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
add a comment
|
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
add a comment
|
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
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
edited Mar 30 at 15:19
answered Mar 30 at 11:18
MuhIbraMuhIbra
12 bronze badges
12 bronze badges
add a comment
|
add a comment
|
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.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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
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