3

I'm using Java driver 3 to work with MongoDB. I have the following documents in a collection.

  {
    "_id" : ObjectId("59231945aefa1a301db180a1"),
    "username" : "off",
    "trx_type" : "pair",
    "amount" : 100000,
    "note" : "testpair 2:2"
  },
  {
    "_id" : ObjectId("591d7a0b03c09b5142fb5602"),
    "amount" : 100000,
    "trx_type" : "pair",
    "note" : "2:2",
    "username" : "ok"
  }

I want to query for this two records.

From command line if I use this: db.transactions.find("note": /2:2/) The following document is included.

{
    "_id" : ObjectId("5925e6a8aefa1a339a8c013f"),
    "username" : "oktrade001",
    "trx_type" : "pairing bonus",
    "time" : NumberLong("1495656104204"),
    "amount" : 100000,
    "note" : "22:22"
}

Tried several things:

This only return exact "2:2", not contains: Document transaction = transactions.find(eq("note", s + ":" + s)).first();

This doesn't work: String note = "/" + s + ":" + s + "/"; Document transaction = transactions.find(eq("note", note)).first();

Read about $regex, couldn't find example queries using this for Mongo Java 3.

Kindly help... Thank you before.

update: so... this is the mongo command I need to do in Java driver: db.transactions.find({"note": {"$regex": /2:2$/}}).pretty() tried this in mongo command line, at got what I wanted. how to write this in Java driver 3+? also... i need to change number 2 to a variable

7
  • Try db.transactions.find({"note": {"$regex": /2:2/}}) Commented May 24, 2017 at 20:30
  • Try Document transaction = transactions.find(regex("note", note)).first(); Commented May 24, 2017 at 22:50
  • @WiktorStribiżew thanks for response, i need the java driver command. please refer to my update above Commented May 25, 2017 at 4:02
  • @Veeram this doesn't work, please refer to my update above... Commented May 25, 2017 at 4:03
  • Try like this: transactions.find(regex("note", ".*" + Pattern.quote(s) + ":" + Pattern.quote(s) + ".*"));. If you need to find the value at the very end of the string. remove the last + ".*". Commented May 25, 2017 at 6:22

3 Answers 3

1

You need to take into account that the regex pattern is anchored by default and thus requires the whole string to match.

Knowing that, you may easily control the position of the match:

  • Start of string:

    transactions.find(regex("note", Pattern.quote(s) + ".*"));

  • End of string:

    transactions.find(regex("note", ".*" + Pattern.quote(s)));

  • Anywhere in a string:

    transactions.find(regex("note", ".*" + Pattern.quote(s) + ".*"));

One note: you will need a DOTALL modifier (an inline version of it is (?s)) if you need to find a match in a string that contains line breaks: transactions.find(regex("note", "(?s).*" + Pattern.quote(s) + ".*"));, or like this: transactions.find(regex("note", "(?s).*" + Pattern.quote(s) + ".*", "s"));. See the regex method docs. The list of supported modifiers (called options in MongoDB) can be checked here.

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

2 Comments

my "s" is not a string, but an int. will Pattern.quote still work?
No, you need to cast the int to a string with Integer.toString(s).
1

so this is what is did:

Document transaction = transactions.find(and(regex("note", ".* " + s + ":" + s + " .*"), eq("username", username))).first();

just added condition for username validation

Comments

0

I believe, the right way to use regex:

transactions.find(regex("note", "^2:2.*"));

do not forget to escape expression if it may contain special symbols:

transactions.find(regex("note", "^" + Pattern.quote("my * expression") + ".*"));

2 Comments

thanks for response, but your answer returns exactly "2:2". what i need is like 2:2, so i'll also get "testpair 2:2"
In this case you need regex("note", ".*2:2.*")

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.