Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use GET to return records based on nested fields

I am trying to retrieve records based on a value in a mongo dataset that is in a nested object.

data is an object and documentId is a field within it and I want to retrieve just the objects within data that have the documentId of "5da713edf0a1645ae95b11oo"

I tried this code

const res = await axios.get('/api/card',{
  params:{
    data:documentId: "5da713edf0a1645ae95b11oo"
  }
});

but it just returns all the records

like image 303
Paul A Avatar asked Mar 08 '26 09:03

Paul A


1 Answers

Try one of these:

const res = await axios.get('/api/card',{
  params:{
    documentId: "5da713edf0a1645ae95b11oo"
  }
});

This would be a GET request to /api/card?documentId=5da713edf0a1645ae95b11oo

or

const res = await axios.get('/api/card',{
  params:{
    data: {
      documentId: "5da713edf0a1645ae95b11oo"
    }
  }
});

This would be a GET request to something like /api/card?data=%7B%22documentId%22%3A%225da713edf0a1645ae95b11oo%22%7D

...where %7B%22documentId%22%3A%225da713edf0a1645ae95b11oo%22%7D is URL encoded version of {"documentId":"5da713edf0a1645ae95b11oo"}

like image 121
dvdsmpsn Avatar answered Mar 10 '26 22:03

dvdsmpsn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!