Mohit Rawat
Mohit Rawat

Reputation: 21

Updating mongodb using multiple threads in java

I have a huge database in which i have to find certain keywords in the "keywords" column. If i find the word i am searching for in the "keywords" column, then i update(increase by 1) another column "Counter" in the same tuple. What i want to do is, search for multiple keywords at the same time using threads, which increase the counter value as soon as it finds the keyword.

For eg: I want to search for "authentication" and "failure". I run 2 threads for that At the end the tuple with "Keywords" column as "Authentication, Failure" should have "Counter" value as 2 And the tuple with "Keywords" column value "Authentication, Passes" should have "Counter" value as 1. And the tuple with "Keywords" column value "Security, Passes" should have "Counter" value as 0.

Thanks in advance for the help.

Upvotes: 0

Views: 1244

Answers (1)

heinob
heinob

Reputation: 19464

If you use $inc on your threads you should get what you want.

EDIT:

Thread 1:

coll.update( { keywords: "authentification" }, { $inc: { counter: 1 } } );

Thread 2:

coll.update( { keywords: "failure" }, { $inc: { counter: 1 } } );

What you have to assure is, that the keywords property is either set with a string or an array. If your source data looks like "Authentication, Passes" you have to parse it and make an array like [ "authentification", "passes" ] out of it.

Upvotes: 1

Related Questions