Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

60 add questions more efficiently to database #63

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion questionGenerator/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package main.java;

import java.util.ArrayList;
import java.util.List;

import main.java.questionGenerator.QuestionGenerator;
import main.java.questionGenerator.question.Question;
import main.java.questionGenerator.question.QuestionType;
Expand Down Expand Up @@ -33,12 +36,17 @@ public static void main(String[] args) {
}

private static void run(QuestionGenerator qg, QuestionType type) {
List<String> questionJSONList = new ArrayList<>();

//Populate JSON list here
for(int i=0; i<3; i++) {
Question question = qg.generateQuestion(type);
question.setNumber(i);
QuestionRepository.getInstance().insertOne(question.getJSON().toString());
questionJSONList.add(question.getJSON().toString());
System.out.println(question.getJSON().toString());
}

QuestionRepository.getInstance().insert(questionJSONList);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.InsertManyOptions;
import com.mongodb.client.result.InsertManyResult;

import java.util.ArrayList;
import java.util.List;

import org.bson.Document;

public class QuestionRepository {
Expand All @@ -22,7 +28,7 @@ public static QuestionRepository getInstance(){
return questionRepository;
}

public boolean insertOne(String questionJSON){
public boolean insert(String questionJSON){
try (MongoClient mongoClient = MongoClients.create(dbConnectionString)) {
MongoDatabase database = mongoClient.getDatabase("questions");

Expand All @@ -35,5 +41,25 @@ public boolean insertOne(String questionJSON){
return false;
}
}

public boolean insert(List<String> questionJSONList){
try (MongoClient mongoClient = MongoClients.create(dbConnectionString)) {
MongoDatabase database = mongoClient.getDatabase("questions");

MongoCollection<Document> collection = database.getCollection("questions");

List<Document> documents = new ArrayList<>();
for (String questionJSON : questionJSONList) {
documents.add(Document.parse(questionJSON));
}

InsertManyResult result = collection.insertMany(documents, new InsertManyOptions().ordered(false)); //unordered write
// Check if all documents were inserted successfully
return result.wasAcknowledged() && result.getInsertedIds().size() == documents.size();
} catch (Exception e) {
System.out.println(e);
return false;
}
}

}
Loading