4

I am trying to get specific data from mongoDB. I have 5 fields that user can make search based on these. Here is my code :

Map<Object, Object> fields = new HashMap<Object, Object>();
if (textField.getText().length() != 0)
    fields.put("FirstName", textField.getText());
if (textField_1.getText().length() != 0)
    fields.put("LastName", textField_1.getText());
if (textField_2.getText().length() != 0)
    fields.put("City", textField_2.getText());
if (textField_3.getText().length() != 0)
    fields.put("EmailAddress", textField_3.getText());
if (textField_4.getText().length() != 0)
    fields.put("PhoneNumber", "NumberLong(" + textField_4.getText() + ")");
query.putAll(fields);
long startTime = System.nanoTime();
DBCursor result = coll.find(query);
ArrayList<ArrayList<Object>> list = createListDataMongo(result); 

All of the search fields works except for phone number. When I have added to the query phone number nothing returns. PhoneNumber is a NumberLong field in my mongoDB database. How can I handle this problem?

1 Answer 1

2
fields.put("PhoneNumber", "NumberLong(" + textField_4.getText() + ")");

searches for documents where PhoneNumber has the value "NumberLong(" + textField_4.getText() + ")" of type text(String).

Please try this.

fields.put("PhoneNumber", Long.parseLong(textField_4.getText()));

And as usual, handle the exceptions.

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

Comments

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.