use of com.mongodb.client.MongoCollection in project spring-data-mongodb by spring-projects.
the class SessionBoundMongoTemplateTests method setUp.
@BeforeEach
public void setUp() {
MongoDatabaseFactory factory = new SimpleMongoClientDatabaseFactory(client, "session-bound-mongo-template-tests") {
@Override
public MongoDatabase getMongoDatabase() throws DataAccessException {
MongoDatabase spiedDatabse = Mockito.spy(super.getMongoDatabase());
spiedDatabases.add(spiedDatabse);
return spiedDatabse;
}
};
session = client.startSession(ClientSessionOptions.builder().build());
this.template = new MongoTemplate(factory);
this.sessionBoundTemplate = new SessionBoundMongoTemplate(session, new MongoTemplate(factory, getDefaultMongoConverter(factory))) {
@Override
protected MongoCollection<Document> prepareCollection(MongoCollection<Document> collection) {
injectCollectionSpy(collection);
return super.prepareCollection(collection);
}
@SuppressWarnings({ "ConstantConditions", "unchecked" })
private void injectCollectionSpy(MongoCollection<Document> collection) {
InvocationHandler handler = Proxy.getInvocationHandler(collection);
Advised advised = (Advised) ReflectionTestUtils.getField(handler, "advised");
for (Advisor advisor : advised.getAdvisors()) {
Advice advice = advisor.getAdvice();
if (advice instanceof SessionAwareMethodInterceptor) {
MongoCollection<Document> spiedCollection = Mockito.spy((MongoCollection<Document>) ReflectionTestUtils.getField(advice, "target"));
spiedCollections.add(spiedCollection);
ReflectionTestUtils.setField(advice, "target", spiedCollection);
}
}
}
};
}
use of com.mongodb.client.MongoCollection in project MyPet by xXKeyleXx.
the class MongoDbRepository method getAllMyPetPlayers.
@Override
@SuppressWarnings("unchecked")
public List<MyPetPlayer> getAllMyPetPlayers() {
MongoCollection playerCollection = this.db.getCollection(Configuration.Repository.MongoDB.PREFIX + "players");
final List<MyPetPlayer> playerList = new ArrayList<>();
playerCollection.find().forEach((Block<Document>) document -> {
MyPetPlayer player = documentToPlayer(document);
if (player != null) {
playerList.add(player);
}
});
return playerList;
}
use of com.mongodb.client.MongoCollection in project MyPet by xXKeyleXx.
the class MongoDbRepository method initStructure.
@SuppressWarnings("unchecked")
private void initStructure() {
db.createCollection(Configuration.Repository.MongoDB.PREFIX + "info");
db.createCollection(Configuration.Repository.MongoDB.PREFIX + "pets");
db.createCollection(Configuration.Repository.MongoDB.PREFIX + "players");
MongoCollection petCollection = db.getCollection(Configuration.Repository.MongoDB.PREFIX + "pets");
petCollection.createIndex(new BasicDBObject("uuid", 1));
petCollection.createIndex(new BasicDBObject("owner_uuid", 1));
MongoCollection playerCollection = db.getCollection(Configuration.Repository.MongoDB.PREFIX + "players");
playerCollection.createIndex(new BasicDBObject("internal_uuid", 1));
playerCollection.createIndex(new BasicDBObject("offline_uuid", 1));
playerCollection.createIndex(new BasicDBObject("mojang_uuid", 1));
Document info = new Document();
updateInfoDocument(info);
MongoCollection infoCollection = db.getCollection(Configuration.Repository.MongoDB.PREFIX + "info");
infoCollection.insertOne(info);
}
use of com.mongodb.client.MongoCollection in project MyPet by xXKeyleXx.
the class MongoDbRepository method addMyPetPlayer.
@SuppressWarnings("unchecked")
public boolean addMyPetPlayer(MyPetPlayer player) {
Document playerDocument = new Document();
setPlayerData(player, playerDocument);
MongoCollection playerCollection = db.getCollection(Configuration.Repository.MongoDB.PREFIX + "players");
playerCollection.insertOne(playerDocument);
return true;
}
use of com.mongodb.client.MongoCollection in project MyPet by xXKeyleXx.
the class MongoDbRepository method updateToV4.
private void updateToV4() {
MongoCollection playerCollection = db.getCollection(Configuration.Repository.MongoDB.PREFIX + "players");
Document filter = new Document();
Document data = new Document("$set", new Document("last_update", System.currentTimeMillis()));
playerCollection.updateMany(filter, data);
}
Aggregations