Home > Software design >  How do I pass multiple values into a query string in my mongoose / mongodb schema?
How do I pass multiple values into a query string in my mongoose / mongodb schema?

Time:12-12

According to Atlas App Services (http.get), I've created a simple API that allows me to query documents using the equal sign "=".

E.g. (field is color and value is red)

https://www.example.com/toys?color=blue

Do you know how to pass multiple values into the URL query string? I've tried to query my api using https://www.example.com/toys?color=['blue','red'] but it does not work. What is the correct URL query string in this case? So that I can adjust my conditions accordingly.

My mongo shell command in this case works so I'm sure mongoDB accepts multiple values.. e.g.

query='{"toys": { "$in": ["blue","red"]}

CodePudding user response:

You can simple JSON.stringify(toys) in query string from Front End and JSON.parse(toys) the JSON in mongoDb.

Example:

color = ['blue', 'red'];

var arrStr = JSON.stringify(color);

var myLink = 'https://www.example.com/toys?color=' arrStr;

CodePudding user response:

You can pass the query parameters in array format from the frontend as shown below.

https://www.example.com/toys?color[]=blue&color[]=red

In your backend API, this array can be access as req.query.color.

  • Related