简体   繁体   中英

How do I return data from mongodb collection in Java

I have a MongoDB database with a collection that looks like this:

+----------+-------------+
| Field    | Type        |
+----------+-------------+
| sha      | varchar(40) |
| filename | text        | 
+----------+-------------+

And I also have a MySQL database with a table that looks like this:

+----------+-------------+
| Field    | Type        |
+----------+-------------+
| sha      | varchar(40) |
| filename | text        | 
+----------+-------------+

The SHA column in the MySQL table contains 50000 records, while the filename column is empty which I want to get from the MongoDB collection above. I want to compare the SHA keys in this table with the SHA keys in the MongoDB collection. The rule needs to be if the SHA exists in the Mongo collection, then pull the filename that corresponds with that SHA key and insert it into the filename field that corresponds with the SHA key in the local MySQL.

How can I return the filename values based on the sha values I have in MySQL? I am just not familiar with how MongoDB queries work, are my Mongodb queries correct?

The current code I have written looks like this:

//Get sha values from mysql and compare with the sha values in mongo and return corresponding filenames
Statement sqlStmt = mysqlConn.createStatement();
HashMap < String, String > shaFileNameMap = new HashMap < String, String > ();

//get sha values from local mysql
String shaQuery = "select * from commit_files";
ResultSet results = sqlStmt.executeQuery(shaQuery);
String sha = null;
String filename = null;

//Store returned sha values in a hashmap
while (results.next()) {
  sha = results.getString("sha");
  filename = results.getString("filename");
  //Store in a map
  shaFileNameMap.put(sha, filename);
}

for (Map.Entry < String, String > entry: shaFileNameMap.entrySet()) {
  String key = entry.getKey();

  //get all filenames matching the list from mongo db and insert into mysql
  DBCollection commits = db.getCollection("commits");
  DBObject query = new BasicDBObject();
  BasicDBObject fields = new BasicDBObject("sha", "" + key + "");
  DBCursor curs = commits.find(query, fields);
  while (curs.hasNext()) {
    DBObject o = curs.next();

    //The code to insert filenames into Mysql goes here
  }
}

My advice: use the current API:

...
MongoClient client = new MongoClient();
MongoCollection<Document> coll = client .getDatabase("xyz").getCollection("myColl");
...
// in the query loop:
FindIterable<Document> res = coll.find(Filters.eq("sha", key);
// and go through the find iterable which has 0 to many entries

The key point is, that the com.mongodb.client.model.Filters class holds everyhing you ever need as a shortcut to create your queries.

API-doc at https://api.mongodb.com/java/current/com/mongodb/client/model/Filters.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM