2

I have multiple mongodb documents which looks like this

    {
    "_id": "001",
    "car_description": "Audi",
    "sales": [
        "India/Mumbai",
        "India/Delhi",
        "India/Chennai",
        "India/Kolkata",
        "US/NYC",
        "US/SF"]
   },
   {
    "_id": "002",
    "car_description": "BMW",
    "sales": [
        "India/Mumbai",
        "India/Delhi",
        "India/Chennai",
        "India/Kolkata",
        "US/NYC",
        "US/SF"]
}

I am trying to get the car_description and sales which happened in India. Final output should be something like this.

{
  {
   "car_description": "Audi",
   "sales": [
        "India/Mumbai",
        "India/Delhi",
        "India/Chennai",
        "India/Kolkata"]
  },
  {
    "car_description": "BMW",
    "sales": [
        "India/Mumbai",
        "India/Delhi",
        "India/Chennai",
        "India/Kolkata"]
  }
}

EDIT : I tried using this but this does not filter out the sales. Instead it gives an error saying "Unrecognized expression '$regexMatch"

db.collection.aggregate([
  {
    "$project": {
      "car_description": 1,
      "sales": 1,
      "sales": {
        "$filter": {
          "input": "$sales",
          "as": "sale",
          "cond": {
            $regexMatch: {
              input: "$$sale",
              regex: "India",
              options: "i"
            }
          }
        }
      }
    }
  }
])

1 Answer 1

1

Check out this snippet to see if it fulfills your need:

db.sales.find({
  "sales": {
    $elemMatch: {
      $regex: "^India\/",
      $options: "i"
    }
  }
},
{
  "car_description": 1,
  "sales": 1
})

https://mongoplayground.net/p/61sqeMXU_yg

EDIT: If you'd like to filter out all regions which are not matched by the regex expression you could try the following aggregate query:

MongoDB >= 4.1.11

db.sales.aggregate([
  {
    $match: {
      "sales": {
        $elemMatch: {
          $regex: "^India\/",
          $options: "i"
        }
      }
    }
  },
  {
    $set: {
      sales: {
        $filter: {
          input: "$sales",
          as: "sale",
          cond: {
            $regexMatch: {
              input: "$$sale",
              regex: "^India\/",
              options: "i"
            }
          }
        }
      }
    }
  }
])

https://mongoplayground.net/p/NGDSVQeXtRI

Older MongoDB releases

db.collection.aggregate([
  {
    $match: {
      "sales": {
        $elemMatch: {
          $regex: "^India\/",
          $options: "i"
        }
      }
    }
  },
  {
    $set: {
      sales: {
        $filter: {
          input: "$sales",
          as: "sale",
          cond: {
            $eq: [
              {
                $toLower: {
                  $arrayElemAt: [
                    {
                      $split: [
                        "$$sale",
                        "/"
                      ]
                    },
                    0
                  ]
                }
              },
              "india"
            ]
          }
        }
      }
    }
  }
])

https://mongoplayground.net/p/19TARSVTaMN

Sign up to request clarification or add additional context in comments.

5 Comments

I see what you're trying to do, but you'd only be able to reproduce it with an aggregate query, as opposed to find.
$regexMatch expression is available since MongoDB 4.2 - aside from development version 4.1.11.
If your issue was solved with this, please accept the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.