Node.js API router query string search for values containing the query string

I've created a custom API for internal use and have query strings used to find specific assets - http://localhost:3000/api/asset?ipaddress=10.1.1.100 ... returns 1 device

What I would like to do is return results for all devices with the ip address that contains the substring in the query string - http://localhost:3000/api/asset?ipaddress=10.1.1 ... returns all devices within this subnet. Right now, this returns 0 devices.

Here's the structure of my router(s) -

router.get('/', verifyToken, function (req, res) {
    const query = {};

    if(req.query.clientcode)
        query.clientcode = req.query.clientcode;
    if(req.query.assetkey)
        query.assetkey = req.query.assetkey;
    if(req.query.name)
        query.name = req.query.name;
    if(req.query.siteid)
        query.siteid = req.query.siteid;
    if(req.query.ipaddress)
        query.ipaddress = req.query.ipaddress;

    asset.find(query,function(err, results) {
        return res.json(results);
      });
});

Open in new window

LVL 30
Britt ThompsonSr. Systems EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
server side should handle this query check how many dot you have
you did not posted server side code handler
0
Britt ThompsonSr. Systems EngineerAuthor Commented:
Just started using Node a couple months ago so you'll have to forgive my ignorance in knowing what to show you when you request the server side code handler. Here's my entire controller for this piece of the API. This is only controller using query strings - minus the update, remove, post.... I can ingest the data and filter it from the front end but as the data set grows I'd prefer to limit the size of my queries with query strings. The database is MongoDB.

import { vT as verifyToken } from '../auth/verifyToken';
import express from 'express';
import bodyParser from 'body-parser';
import { asset } from './asset';

const router = express.Router();
router.use(bodyParser.json({ limit:'50mb' }));
router.use(bodyParser.urlencoded({ extended:true ,limit:'50mb', parameterLimit:50000 }));

router.get('/', verifyToken, function (req, res) {
    const query = {};

    if(req.query.clientcode)
        query.clientcode = req.query.clientcode;
    if(req.query.assetkey)
        query.assetkey = req.query.assetkey;
    if(req.query.name)
        query.name = req.query.name;
    if(req.query.siteid)
        query.siteid = req.query.siteid;
    if(req.query.ipaddress)
        query.ipaddress = req.query.ipaddress;

    asset.find(query,function(err, results) {
        return res.json(results);
      });
});

export const assetController = router;

Open in new window

0
leakim971PluritechnicianCommented:
don't worry, it's my mistake : I did not checked the topics of your question and thought we was on client side...
the key of your issue is in the asset(.js(?) file, could you post it ?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Britt ThompsonSr. Systems EngineerAuthor Commented:
import mongoose from 'mongoose';

const assetSchema = new mongoose.Schema({
    assignedto: String,
    clientcode: String,
    siteid: Number,
    name: String,
    status: String,
    type: Number,
    model: String,
    manufacturer: String,
    location: String,
    assettag: String,
    assetkey: String,
    customerassettag: String,
    serial: String,
    firmware: String,
    ipaddress: String,
    externalipaddress: String,
    macaddress: String,
    loginurl: String,
    operatingsystem: String,
    servicepack: String,
    cputype: String,
    cpu: String,
    ram: String,
    domain: String,
    role: String,
    logicaldrives:[
        {
            id: String,
            capacity: Number,
            spacefree: Number
        }
    ],
    physicaldrives:[
        {
            id: String,
            capacity: Number
        }
    ],
    productkey: String,
    purchasedate: Date,
    warrantyenddate: Date,
    service: String,
    notes: String,
    software: String,
    datecreated: Date,
    datemodified: Date
});

mongoose.model('asset', assetSchema);
export const asset = mongoose.model('asset');

Open in new window

0
Julian HansenCommented:
You need to use a regex for the ip address field (refer https://docs.mongodb.com/manual/reference/operator/query/regex/)
[NOTE: UNTESTED CODE - PROVIDED TO DEMONSTRATE CONCEPT]
...
if(req.query.ipaddress)
        query.ipaddress = {$regex: req.query.ipaddress + '.*'};

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
leakim971PluritechnicianCommented:
try the following :
import { vT as verifyToken } from '../auth/verifyToken';
import express from 'express';
import bodyParser from 'body-parser';
import { asset } from './asset';

const router = express.Router();
router.use(bodyParser.json({ limit:'50mb' }));
router.use(bodyParser.urlencoded({ extended:true ,limit:'50mb', parameterLimit:50000 }));

router.get('/', verifyToken, function (req, res) {
    const query = {};

    if(req.query.clientcode)
        query.clientcode = req.query.clientcode;
    if(req.query.assetkey)
        query.assetkey = req.query.assetkey;
    if(req.query.name)
        query.name = req.query.name;
    if(req.query.siteid)
        query.siteid = req.query.siteid;
    if(req.query.ipaddress && (req.query.ipaddress.split(".") == 3))
        query.ipaddress = new RegExp("^" + req.query.ipaddress + "\..{1,3}");

    if(req.query.ipaddress && (req.query.ipaddress.split(".") == 4))
        query.ipaddress = req.query.ipaddress;

    asset.find(query,function(err, results) {
        return res.json(results);
      });
});

export const assetController = router;

Open in new window

0
Britt ThompsonSr. Systems EngineerAuthor Commented:
Sorry for the slow response here. I'll give this a try today if I have a minute. Thanks!
0
Britt ThompsonSr. Systems EngineerAuthor Commented:
Worked perfectly. Thanks!

The other solution returned all results no matter what for some reason.
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Node.js

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.