how do i write a query in mongodb where keys are present inside an object?

You can use the $or-clausel and pass in all the expressions you want to match.

const localName = "de_DE";

YourCollection.find({
  $or: [
    {
      "value.name.locale": localName 
    },
    {
      "value.customText.locale": localName 
    },
    {
      "value.newAttribute.locale": localName 
    },
   // repeat for the rest of the fields
  ]
});

Here’s an example on mongoplayground: https://mongoplayground.net/p/5YAJQvWjMJq

If you only want to match a document if all of above fields contain "de_DE" you can simply exchange $or for $and.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top