Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

filter a query string with comparison operator

//if need to find ratings greater then 3
//The query string may be:
//127.0.0.1:8000/api/v1/posts?ratings[gt]=3
//if we check the req.query
//{ ratings: { gt: '3' } }


exports.getPosts = async (req, res) => {
try {
    const queryObj = { ...req.query };
    let queryStr = JSON.stringify(queryObj)
    queryStr = queryStr.replace(/(gt|gte|lt|lte|eq|ne)/g, match => `$${match}`);
    // queryStr  : {"ratings":{"$gt":"3"}}
    const posts = await Post.find(JSON.parse(queryStr));
    res.json({
        status: 'success',
        data: {
            posts
        }
    })
} catch (err) {
    res.status(401).json({
        status: 'error',
        message: 'Error in post finding',
        error: err
    })
}}
Comment

PREVIOUS NEXT
Code Example
Javascript :: i need to add content-type accept form data using node.js in middelware 
Javascript :: component not registered correctly 
Javascript :: How to apply CodeMirror in Bootstrap Modal 
Javascript :: Using Math Functions in an IF statement 
Javascript :: F:JavascriptOOP 
Javascript :: faker link for json post req 
Javascript :: jequery mose up 
Javascript :: check if anagram 
Javascript :: how to get selected option attribute value in jquery 
Javascript :: javascript for sub set 
Javascript :: how to change text in paragraph javascript 
Javascript :: javascript check if string contains capital letter 
Javascript :: CELEBRITY PROBLEM 2 gfg 7-18-21 
Javascript :: settimeout react native focus text input 
Javascript :: discord.js change role permissions 
Javascript :: handle fetch error 
Javascript :: how to chnge line in browser js 
Javascript :: Make stepper with 2 values javascript 
Javascript :: Add rows to the table dynamically with the use of vue.js 
Javascript :: Node temp = new Node(6, head, head.getNext()); head.setNext(temp); temp.getNext().setPrev(temp); Node temp1 = tail.getPrev(); tail.setPrev(temp1.getPrev()); temp1.getPrev().setNext(tail); 
Javascript :: remove falsy values from object lodash 
Javascript :: add if condition in map react 
Javascript :: two jq events 
Javascript :: count same product with price in angular 
Javascript :: push array into another array at random positions javascript 
Javascript :: https://ssl.clickbank.net/order/orderform.html?time=1637595355&vvvv=62766b313233&item=6&cbfid=35141&cbf=YQYI4X5NDF&vvar=cbfid%3D35141&corid=1ee8f46f-018e-4c7d-ba0c-733317d97f43 
Javascript :: JavaScript Program to illustrate split() function 
Javascript :: javascript html find the largest number among 2 
Javascript :: Javascript index,length,push,pop,shift,unshift 
Javascript :: how to send multiple values in event in javascript 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =