Fuzzy search problem of MongoDb in express

scenario:
the front-end vue separates axios and passes a parameter to the back-end. You want to use the back-end to realize the fuzzy search function. After filtering the data, you return it to the foreground and get the fuzzy query condition of MongoDb {name:/ field /}.
question:
I would like to ask / a / how to write the parameter in the middle of the slash if it is a variable. I just tried a lot. Write directly into the string"{name:/ variable /}"or"{"name": / variable /}"to find out all the data.
wants to use JSON.parse to change a string into an object, but it can only be changed to {name:"/ variable /"} to find out all the data. Use {"name": / variable /} in the cmd command to vaguely search

to put it simply, you want to use the JSON object as a condition, but / variable / you can"t do it without quotation marks. Ask for help. What you want is the object {name:/ condition /}, but you can only get {name:"/ condition /}

.

and at present, I have learned less, so the knowledge is limited here, and there are other ways to expand it.


if the operator of a fuzzy query is added at the back end, your idea is called an injection attack.


this should work

user = { name: 'promo' }
db.user.find({ name: { $regex: '.*' + user.name + '.*' } })

when it comes to regular expressions ($regex)
, please refer to the official document https://docs.mongodb.com/manu.

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eab474-2040.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eab474-2040.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?