1

I am trying to make the following mongodb query on powershell:

collection.find({"field1":"valueA","field2":"valueB","field3":/subStr_ValueC/})

I tried to do the following but doesn't work:

$query = @{
            "field1" = $notification["A"]
            "field2" = $notification["B"]
            "field3" = "/" + $notification["C"] + "/"
        }

$cursor = $collection.find([MongoDB.Driver.QueryDocument]$query)

Any idea, how to do this?

thank you very much

1 Answer 1

2

Give this a try:

$query = @{
  'field1' = $notification['A']
  'field2' = $notification['B']
  'field3' = @{ '$regex' = '.*' + $notification['C'] + '.*'; '$options' = 'i' }
}

$cursor = $collection.find([MongoDB.Driver.QueryDocument]$query)
Sign up to request clarification or add additional context in comments.

5 Comments

what do you have in $notification["C"]? Try with "field3" = @{ "$regex" = ".*"; "$options" = "i" } to see if this works.
field3 is a string. It is giving me this error Duplicate keys '' are not allowed in hash literals. using your 2 suggestions
I forgot that it tries to evaluate things in double quotes. Try the updated version (with single quotes).
Yep that worked, why id did not work with double quotes? also I'm wondering if the "i" option is taking more resources as it will compare also capital leters?
Great. You can omit '$options' = 'i' if you want case sensitivity. When you are using double quotes powershell replaces $regex and $options with respective variable values (in this case no variable $regex and $option was there and it resulted in an empty string)

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.