use of de.bwaldvogel.mongo.backend.memory.MemoryBackend in project books by aidanwhiteley.
the class MongoJavaServerConfig method mongoClient.
@Override
@NonNull
public MongoClient mongoClient() {
preProdWarnings.displayMongoJavaServerWarningMessage();
MongoServer server = new MongoServer(new MemoryBackend());
// bind on a random local port
InetSocketAddress serverAddress = server.bind();
return MongoClients.create("mongodb://" + serverAddress.getHostName() + ":" + serverAddress.getPort());
}
use of de.bwaldvogel.mongo.backend.memory.MemoryBackend in project selenium_java by sergueik.
the class BasicMongoServerClient method main.
public static void main(String[] args) {
// Creating a Mongo client
server = new MongoServer(new MemoryBackend());
// bind on a random local port
InetSocketAddress serverAddress = server.bind();
// Creating a Mongo client
client = new MongoClient(new ServerAddress(serverAddress));
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb", "password".toCharArray());
System.out.println("Connected to the database successfully");
// Accessing the database
database = client.getDatabase("dummy");
System.out.println("Credentials ::" + credential);
// Creating a collection
try {
database.createCollection(collectionName);
System.err.println(String.format("Collection \"%s\" created successfully", collectionName));
} catch (MongoCommandException e) {
System.err.println(String.format("Collection \"%s\" already exists", collectionName));
} catch (Exception e) {
System.out.println(String.format("Exception creating collection \"%s\":", collectionName) + e.toString());
throw new RuntimeException(e);
}
// Retrieving a collection
collection = database.getCollection(collectionName);
System.out.println(String.format("Collection \"%s\" selected successfully", collectionName));
// Listing all collections in the database
for (String name : database.listCollectionNames()) {
System.out.println(name);
}
Document document = new Document("title", "MongoDB").append("id", 1).append("description", "database").append("likes", 100).append("url", "http://www.tutorialspoint.com/mongodb/").append("by", "tutorials point");
collection.insertOne(document);
System.out.println("Document inserted successfully");
collection.updateOne(Filters.eq("id", 1), Updates.set("likes", 150));
System.out.println("Document update successfully...");
// Getting the iterable object
FindIterable<Document> iterDoc = collection.find();
int i = 1;
// Getting the iterator
Iterator<Document> it = iterDoc.iterator();
while (it.hasNext()) {
System.out.println(it.next());
i++;
}
// Deleting the documents
collection.deleteOne(Filters.eq("id", 1));
System.out.println("Document deleted successfully...");
// Dropping a Collection
collection.drop();
System.out.println(String.format("Collection \"%s\" dropped successfully", collectionName));
client.close();
server.shutdown();
}
use of de.bwaldvogel.mongo.backend.memory.MemoryBackend in project immutables by immutables.
the class MongoInstance method create.
static MongoInstance create() {
final String uri = System.getProperty("mongo");
if (uri != null) {
// connect to remote mongo server
return new MongoInstance(MongoClients.create(uri));
}
final MongoServer server = new MongoServer(new MemoryBackend());
final InetSocketAddress address = server.bind();
final Closer closer = Closer.create();
closer.register(server::shutdownNow);
final MongoClient client = MongoClients.create(String.format("mongodb://127.0.0.1:%d", address.getPort()));
return new MongoInstance(client, closer);
}
use of de.bwaldvogel.mongo.backend.memory.MemoryBackend in project immutables by immutables.
the class MongoContext method create.
public static MongoContext create() {
final String uri = System.getProperty("mongo");
final Closer closer = Closer.create();
if (uri != null) {
// remote mongo server
return new MongoContext(new MongoClient(new MongoClientURI(uri)), closer);
}
final MongoServer server = new MongoServer(new MemoryBackend());
closer.register(new Closeable() {
@Override
public void close() throws IOException {
server.shutdownNow();
}
});
final MongoClient client = new MongoClient(new ServerAddress(server.bind()));
final MongoContext context = new MongoContext(client, closer);
return context;
}
use of de.bwaldvogel.mongo.backend.memory.MemoryBackend in project presto by prestodb.
the class TestMongoPlugin method start.
@BeforeClass
public void start() {
server = new MongoServer(new MemoryBackend());
InetSocketAddress address = server.bind();
seed = String.format("%s:%d", address.getHostString(), address.getPort());
}
Aggregations