0

I have a collection in MongoDB that is structured like this:

  "5d33488672886334cd21904xx": {
    "ownerId": "5d333551k99951924fb3208",
    "createdAt": 1582456098,
    ....phone
    ....email
    ....etc
    "mainActivities": {
      "dogWalking": true
    },
    "secActivities": {
      "washing": true,
      "houseSitting": true,
      "dogSitting": true,
      "training": true,
      "trainingEquipment": true,
      "selectionAdvice": true,
      "institutionsBusinesses": true
    }
}

  "5d33488672886334cd21904xx": {
    "ownerId": "5d333d344d46ed924fb3208",
    "createdAt": 99999995,
    ....phone
    ....email
    ....etc
    "mainActivities": {
      "dogWalking": true
    },
    "secActivities": {
      "washing": false,
      "houseSitting": false,
      "dogSitting": false,
      "training": true,
      "trainingEquipment": false,
      "selectionAdvice": true,
      "institutionsBusinesses": true
    }
}

i am sending filters from the front end like so :

{
  "filters": {
    "mainActivities": {
      "dogWalking": true
    },
    "secActivities": {
      "washing":           false,
      "houseSitting":      true,
      "dogSitting":        true,
      "training":          true,
      "trainingEquipment": true,
      "selectionAdvice":   false,
      "institutionsBusinesses": true
    }
  }
}

i want to get all the documents that answer the filters and not EXACT match .

Example: if i am a dog walker service named "Xdog" , who give all the 6/6 activities (secActivities)

in the front end , when user select 2/6 filters i miss this "Xdog" service.

i get to a function who return only the document that specify EXACT match and its not good because i am missing the services who actually answer this 2/6 filters..

right now i am sending and spreading the "filterBy" variable into the find() function .

Any suggestion would be great i am weak in Databases commands.

Sorry for my poor English!

1 Answers1

0

The way to do partial match is with $or, but I don't think that will do what you actually want.

The example query you showed above is looking for dog walking services that do not provide washing or selectionAdvice services. Your description suggests that you do not want to consider these 2 services because the user did not specify them. In that case, leave these 2 out of the query entirely, using the user selections to add only the relevant fields, like:

.find({
    "mainActivities": {
      "dogWalking": true
    },
    "secActivities": {
      "houseSitting":      true,
      "dogSitting":        true,
      "training":          true,
      "trainingEquipment": true,
      "institutionsBusinesses": true
    }
  }

If the filters object is already built, you would need to remove the properties that are false. See How do I remove a property from a JavaScript object?

Joe
  • 25,000
  • 3
  • 22
  • 44