本文整理汇总了Java中com.allanbank.mongodb.MongoClient.getDatabase方法的典型用法代码示例。如果您正苦于以下问题:Java MongoClient.getDatabase方法的具体用法?Java MongoClient.getDatabase怎么用?Java MongoClient.getDatabase使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.allanbank.mongodb.MongoClient
的用法示例。
在下文中一共展示了MongoClient.getDatabase方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testObjectIdParsingMatches
import com.allanbank.mongodb.MongoClient; //导入方法依赖的package包/类
/**
* Tests that the parsing of an ObjectId in the shell matches that by the
* driver.
*/
@Test
public void testObjectIdParsingMatches() {
final MongoClientConfiguration config = new MongoClientConfiguration();
config.addServer(new InetSocketAddress("127.0.0.1", 27017));
config.setMaxConnectionCount(1);
final MongoClient mongo = MongoFactory.createClient(config);
final MongoDatabase db = mongo.getDatabase("test");
final MongoCollection collection = db.getCollection("test");
// Make sure the collection is created and empty.
db.createCollection("test", BuilderFactory.start());
collection.delete(BuilderFactory.start(), Durability.ACK);
final ObjectId id = new ObjectId(0x11223344, 0x5566778899001122L);
final ObjectIdElement element = new ObjectIdElement("_id", id);
assertEquals("'_id' : ObjectId('112233445566778899001122')",
element.toString());
final Document doc = BuilderFactory.start().add(element).build();
assertEquals("{ '_id' : ObjectId('112233445566778899001122') }",
doc.toString());
final ManagedProcess mp = ourTestSupport
.run(null, "mongo", "localhost:27017/test", "-eval",
"db.test.insert( { '_id' : ObjectId('112233445566778899001122') } );");
mp.waitFor();
// Now pull the document back and make sure it matches what we expect.
final Document result = collection.findOne(BuilderFactory.start());
assertEquals(doc, result);
IOUtils.close(mongo);
}
示例2: main
import com.allanbank.mongodb.MongoClient; //导入方法依赖的package包/类
/**
* Runs a create/modify/delete on a collection.
*
* @param args
* Command line arguments. Expect the MongoDB URL, database and
* collection name as the first, second and third argument.
* @throws InterruptedException
* If the thread is interrupted.
* @throws IOException
* On a failure to close the MongoDB client.
*/
public static void main(String[] args) throws InterruptedException,
IOException {
if (args.length < 3) {
System.out.println("Usage: java " + ModifyIt.class.getName()
+ " <mongodb-url> <database> <collection>");
System.exit(1);
}
MongoClient client = MongoFactory.createClient(args[0]);
MongoDatabase database = client.getDatabase(args[1]);
MongoCollection collection = database.getCollection(args[2]);
String id = new ObjectId().toHexString();
Document query = BuilderFactory.start().add("_id", id).build();
collection.insert(Durability.ACK, query);
for (int i = 0; i < 10; ++i) {
DocumentBuilder update = BuilderFactory.start();
update.push("$inc").add("i", 1);
collection.update(query, update, false, false, Durability.ACK);
Thread.sleep(100);
}
collection.delete(query, Durability.ACK);
client.close();
}
示例3: main
import com.allanbank.mongodb.MongoClient; //导入方法依赖的package包/类
/**
* Runs a single watcher on a collection.
*
* @param args
* Command line arguments. Expect the MongoDB URL, database and
* collection name.
* @throws InterruptedException
* If the thread is interrupted.
*/
public static void main(String[] args) throws InterruptedException {
if (args.length < 3) {
System.out.println("Usage: java " + WatchIt.class.getName()
+ " <mongodb-url> <database> <collection>");
System.exit(1);
}
MongoClient client = MongoFactory.createClient(args[0]);
MongoDatabase database = client.getDatabase(args[1]);
MongoCollection collection = database.getCollection(args[2]);
Watcher watcher = new Watcher(client, collection,
Pattern.compile(".*"), new WatchListener() {
@Override
public void changed(Operation op, String context,
Document document) {
if (op == Operation.DELETE) {
System.out.println(context + ": " + op);
}
else {
System.out.println(context + ": " + op + ": "
+ document);
}
}
});
watcher.start();
while (true) {
Thread.sleep(10000000L);
}
}
示例4: main
import com.allanbank.mongodb.MongoClient; //导入方法依赖的package包/类
/**
* Runs a single producer on the queue.
*
* @param args
* The for the producer. Expect the MongoDBURL, database and
* collection/queue name
* @throws InterruptedException
* If the producer is interrupted.
*/
public static void main(String[] args) throws InterruptedException {
if (args.length < 3) {
System.out.println("Usage: java " + Producer.class.getName()
+ " <mongodb-url> <database> <queue/collection>");
System.exit(1);
}
MongoClient client = MongoFactory.createClient(args[0]);
String dbName = args[1];
String collectionName = args[2];
MongoDatabase db = client.getDatabase(dbName);
MongoCollection collection = db.getCollection(collectionName);
int count = 0;
UUID producerIdentifier = UUID.randomUUID();
DocumentBuilder builder = BuilderFactory.start();
while (true) {
builder.reset();
builder.add("_id", new ObjectId());
builder.add("producer", producerIdentifier);
builder.add("count", count++);
collection.insert(builder);
TimeUnit.MILLISECONDS.sleep(100);
if ((count % 10) == 0) {
System.out.println(count);
}
}
}
示例5: testMongoTimeStampParsingMatches
import com.allanbank.mongodb.MongoClient; //导入方法依赖的package包/类
/**
* Tests that the parsing of an ObjectId in the shell matches that by the
* driver.
*/
@Test
public void testMongoTimeStampParsingMatches() {
int seconds = 0x12345 * 1000; // Times are truncated.
final int offset = 0xAB;
final MongoClientConfiguration config = new MongoClientConfiguration();
config.addServer(new InetSocketAddress("127.0.0.1", 27017));
config.setMaxConnectionCount(1);
final MongoClient mongo = MongoFactory.createClient(config);
final MongoDatabase db = mongo.getDatabase("test");
final MongoCollection collection = db.getCollection("test");
// Make sure the collection is created and empty.
db.createCollection("test", BuilderFactory.start());
collection.delete(BuilderFactory.start(), Durability.ACK);
final MongoTimestampElement element = new MongoTimestampElement("_id",
0x00012345000000ABL);
assertEquals("'_id' : Timestamp(" + seconds + ", " + offset + ")",
element.toString());
final Document doc = BuilderFactory.start().add(element).build();
assertEquals("{ '_id' : Timestamp(" + seconds + ", " + offset + ") }",
doc.toString());
ManagedProcess mp = ourTestSupport.run(null, "mongo",
"localhost:27017/test", "-eval",
"db.test.insert( { '_id' : Timestamp(" + seconds + ", "
+ offset + ") } );");
mp.waitFor();
// Now pull the document back and make sure it matches what we expect.
Document result = collection.findOne(BuilderFactory.start());
// For 2.4 the shell parsing changes to be seconds.
if (!doc.equals(result)) {
seconds = 0x12345;
collection.delete(BuilderFactory.start(), Durability.ACK);
mp = ourTestSupport.run(null, "mongo", "localhost:27017/test",
"-eval", "db.test.insert( { '_id' : Timestamp(" + seconds
+ ", " + offset + ") } );");
mp.waitFor();
result = collection.findOne(BuilderFactory.start());
}
assertEquals(doc, result);
IOUtils.close(mongo);
}
示例6: main
import com.allanbank.mongodb.MongoClient; //导入方法依赖的package包/类
/**
* Initializes the queue by creating a new capped collection and creating a
* new cursor on it. Prints the cursor document to standard out.
*
* @param args
* Command line arguments. Expect the MongoDB URL, database and
* collection/queue name.
* @throws IOException
* On a failure to close the connection to MongoDB.
*/
public static void main(String[] args) throws IOException {
if (args.length < 3) {
System.out.println("Usage: java " + InitializeQueue.class.getName()
+ " <mongodb-url> <database> <queue/collection>");
System.exit(1);
}
MongoClient client = MongoFactory.createClient(args[0]);
String dbName = args[1];
String collectionName = args[2];
MongoDatabase db = client.getDatabase(dbName);
MongoCollection collection = db.getCollection(collectionName);
collection.drop();
db.createCappedCollection(collectionName, 100000000L);
// For a tailable cursor to initialize we need at least a single
// document in the collection.
collection.insert(BuilderFactory.start().add("_id", "seed"));
Find.Builder builder = new Find.Builder(BuilderFactory.start());
builder.setTailable(true);
// builder.setAwaitData(false); // Uncomment if not using patch for SERVER-8602
MongoIterator<Document> cursor = collection.find(builder.build());
// Graceful shutdown of the iterator locally but not on the server.
cursor.stop();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
collection = db.getCollection("lookup");
collection.delete(BuilderFactory.start().add("_id", collectionName));
collection.insert(BuilderFactory.start().add("_id", collectionName)
.add("cursor", cursor.asDocument()));
// Printout the cursor document.
System.out.println("Queue created: " + collectionName);
System.out.println(collection
.findOne(BuilderFactory.start().add("_id", collectionName))
.get("cursor").getValueAsObject());
client.close();
}