當前位置: 首頁>>代碼示例>>Java>>正文


Java MongoClient.getDatabase方法代碼示例

本文整理匯總了Java中com.mongodb.MongoClient.getDatabase方法的典型用法代碼示例。如果您正苦於以下問題:Java MongoClient.getDatabase方法的具體用法?Java MongoClient.getDatabase怎麽用?Java MongoClient.getDatabase使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.mongodb.MongoClient的用法示例。


在下文中一共展示了MongoClient.getDatabase方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: failedInputOfComment

import com.mongodb.MongoClient; //導入方法依賴的package包/類
@Test
public void failedInputOfComment() throws IOException {
    String json = "{ plantId: \"58d1c36efb0cac4e15afd27\", comment : \"Here is our comment for this test\" }";

    assertFalse(plantController.addComment(json, "second uploadId"));

    MongoClient mongoClient = new MongoClient();
    MongoDatabase db = mongoClient.getDatabase(databaseName);
    MongoCollection<Document> plants = db.getCollection("plants");

    FindIterable findIterable = plants.find();
    Iterator iterator = findIterable.iterator();
    while(iterator.hasNext()){
        Document plant = (Document) iterator.next();
        List<Document> plantComments = (List<Document>) ((Document) plant.get("metadata")).get("comments");
        assertEquals(0,plantComments.size());
    }
}
 
開發者ID:UMM-CSci-3601-S17,項目名稱:digital-display-garden-iteration-4-dorfner-v2,代碼行數:19,代碼來源:TestPlantComment.java

示例2: MongoConnector

import com.mongodb.MongoClient; //導入方法依賴的package包/類
private MongoConnector(String connectURIStr) throws DiagnosticException {
  MongoClientURI clientURI = new MongoClientURI(connectURIStr);
  client = new MongoClient(clientURI);
  String dbName = clientURI.getDatabase() == null ? MONITOR_DB : clientURI.getDatabase();
  db = client.getDatabase(dbName);

}
 
開發者ID:YagelNasManit,項目名稱:environment.monitor,代碼行數:8,代碼來源:MongoConnector.java

示例3: UserController

import com.mongodb.MongoClient; //導入方法依賴的package包/類
public UserController(String dbName) throws IOException {
    // Set up our server address
    // (Default host: 'localhost', default port: 27017)
    // ServerAddress testAddress = new ServerAddress();

    // Try connecting to the server
    //MongoClient mongoClient = new MongoClient(testAddress, credentials);
    MongoClient mongoClient = new MongoClient(); // Defaults!

    // Try connecting to a database
    MongoDatabase db = mongoClient.getDatabase(dbName);

    userCollection = db.getCollection("users");
}
 
開發者ID:UMM-CSci-3601-S17,項目名稱:digital-display-garden-iteration-2-spraguesanborn,代碼行數:15,代碼來源:UserController.java

示例4: connect

import com.mongodb.MongoClient; //導入方法依賴的package包/類
private static void connect(String URI){
    //Change MongoDB's logger settings
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    rootLogger = loggerContext.getLogger("org.mongodb.driver");
    rootLogger.setLevel(Level.INFO);

    //Setup Connection
    MongoClientURI connectionString = new MongoClientURI(URI);
    MongoClient mongoClient = new MongoClient(connectionString);
    MongoDatabase database = mongoClient.getDatabase("media_information_service_db");
    collection = database.getCollection("messages");

}
 
開發者ID:LithiumSR,項目名稱:media_information_service,代碼行數:14,代碼來源:MongoDBInterface.java

示例5: clearAndPopulateDB

import com.mongodb.MongoClient; //導入方法依賴的package包/類
@Before
public void clearAndPopulateDB() {
    MongoClient mongoClient = new MongoClient();
    MongoDatabase db = mongoClient.getDatabase("testingdb");
    userDocuments = db.getCollection("users");
    userDocuments.drop();
    List<Document> testUsers = new ArrayList<>();
    testUsers.add(Document.parse("{\n" +
            "                    name: \"Chris\",\n" +
            "                    age: 25,\n" +
            "                    company: \"UMM\",\n" +
            "                    email: \"[email protected]\"\n" +
            "                }"));
    testUsers.add(Document.parse("{\n" +
            "                    name: \"Pat\",\n" +
            "                    age: 37,\n" +
            "                    company: \"IBM\",\n" +
            "                    email: \"[email protected]\"\n" +
            "                }"));
    testUsers.add(Document.parse("{\n" +
            "                    name: \"Jamie\",\n" +
            "                    age: 37,\n" +
            "                    company: \"Frogs, Inc.\",\n" +
            "                    email: \"[email protected]\"\n" +
            "                }"));
    userDocuments.insertMany(testUsers);
}
 
開發者ID:UMM-CSci-3601-S17,項目名稱:digital-display-garden-iteration-4-dorfner-v2,代碼行數:28,代碼來源:MongoSpec.java

示例6: main

import com.mongodb.MongoClient; //導入方法依賴的package包/類
public static void main(String args[]) {

        ConsoleLogger cl = new ConsoleLogger("test");

        cl.setDebugable(true);

        UAVServer.instance().setLog(cl);

        UAVServer.instance().putServerInfo(CaptureConstants.INFO_APPSERVER_VENDOR, ServerVendor.TOMCAT);

        MongoClientHookProxy p = new MongoClientHookProxy("test", Collections.emptyMap());

        p.doInstallDProxy(null, "testApp");

        MongoClient client = new MongoClient();
        client.listDatabaseNames().first();
        MongoDatabase db = client.getDatabase("apphubDataStore");
        db.listCollectionNames().first();
        MongoCollection<Document> collection = db.getCollection("test");
        collection.listIndexes().first();
        Document doc = new Document("name", "Amarcord Pizzeria")
                .append("contact",
                        new Document("phone", "264-555-0193").append("email", "[email protected]")
                                .append("location", Arrays.asList(-73.88502, 40.749556)))
                .append("stars", 2).append("categories", Arrays.asList("Pizzeria", "Italian", "Pasta"));
        collection.insertOne(doc);
        collection.find().first();

        MongoClient client2 = new MongoClient("localhost:27017");
        db = client2.getDatabase("apphubDataStore");
        db.listCollectionNames().first();
        collection = db.getCollection("test");
        collection.listIndexes().first();

        client.close();
        client2.close();
    }
 
開發者ID:uavorg,項目名稱:uavstack,代碼行數:38,代碼來源:DoTestMongoClientProxy.java

示例7: testMongo1

import com.mongodb.MongoClient; //導入方法依賴的package包/類
@POST
@Path("testMongo1")
public void testMongo1(String jsonString) {

    MongoClient client = new MongoClient();
    client.listDatabaseNames().first();
    MongoDatabase db = client.getDatabase("apphubDataStore");
    db.listCollectionNames().first();
    MongoCollection<Document> collection = db.getCollection("test");
    collection.listIndexes().first();
    Document doc = new Document("name", "Amarcord Pizzeria")
            .append("contact",
                    new Document("phone", "264-555-0193").append("email", "[email protected]")
                            .append("location", Arrays.asList(-73.88502, 40.749556)))
            .append("stars", 2).append("categories", Arrays.asList("Pizzeria", "Italian", "Pasta"));
    collection.insertOne(doc);
    collection.find().first();

    MongoClient client2 = new MongoClient("localhost:27017");
    db = client2.getDatabase("apphubDataStore");
    db.listCollectionNames().first();
    collection = db.getCollection("test");
    collection.listIndexes().first();

    client.close();
    client2.close();
}
 
開發者ID:uavorg,項目名稱:uavstack,代碼行數:28,代碼來源:TestRestService.java

示例8: testUnsetUpdate

import com.mongodb.MongoClient; //導入方法依賴的package包/類
@Test
public void testUnsetUpdate() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new UpdateMongo());
    addMongoService(runner);
    runner.setProperty(MongoProps.DATABASE, MONGO_DATABASE_NAME);
    runner.setProperty(MongoProps.COLLECTION, "insert_test");
    runner.setProperty(MongoProps.UPDATE_QUERY_KEYS, "d.id");
    runner.setProperty(MongoProps.UPDATE_KEYS, "d.g");
    runner.setProperty(MongoProps.UPDATE_OPERATOR, "$unset");

    String contents = FileUtils.readFileToString(Paths.get("src/test/resources/update_payload.json").toFile());

    runner.enqueue(contents.getBytes());
    runner.run();

    runner.assertTransferCount(AbstractMongoProcessor.REL_FAILURE, 0);
    runner.assertTransferCount(AbstractMongoProcessor.REL_SUCCESS, 1);

    // Verify whether the update was made
    MongoClient client = mongo.getMongoClient();
    MongoDatabase db = client.getDatabase(MONGO_DATABASE_NAME);
    if (db != null) {
        MongoCollection<Document> collection = db.getCollection("insert_test");
        Document query = buildQuery("d.id", (JsonObject) JSON_PROVIDER.parse(contents));
        assertEquals(collection.count(query.append("d.g", new Document("$exists", true))), 0);
    }
}
 
開發者ID:Asymmetrik,項目名稱:nifi-nars,代碼行數:28,代碼來源:UpdateMongoIT.java

示例9: run

import com.mongodb.MongoClient; //導入方法依賴的package包/類
/**
 * Konstruktor stellt Verbindung zu MongoDB-Server her
 * @param host Die Adresse des Servers.
 * @param database Die zu benutzende Datenbank.
 * @param user Der zu benutzende Benutzer.
 * @param password Das zu benutzende Passwort des Benutzers.
 */
public void run()
{
    System.out.println("Starting MongoDB Manager...");
    // Es wird ein neuer MongoClient mit den Parametern host, database, user und password erstellt
    MongoClientURI uri = new MongoClientURI("mongodb://" + user + ":" + password + "@" + host + "/" + database); //
    client = new MongoClient(uri);

    // Es wird die Datenbank mit dem Wert der Variable
    db = client.getDatabase(database);
    if(db.getCollection("ratings") == null) db.createCollection("ratings");
}
 
開發者ID:Sigma-News,項目名稱:Backend,代碼行數:19,代碼來源:MongoDBManager.java

示例10: MongoBulkWriter

import com.mongodb.MongoClient; //導入方法依賴的package包/類
public MongoBulkWriter(String URI, String namespace)
{
	logger = LoggerFactory.getLogger(MongoBulkWriter.class);
	logger.info("Connecting to " + URI );
	mongoClient = new MongoClient(new MongoClientURI(URI));
	String[] parts = namespace.split("\\.");
	db = mongoClient.getDatabase(parts[0]);
	collection = db.getCollection(parts[1]);
	
	ops = new ArrayList<WriteModel<Document>>();
}
 
開發者ID:johnlpage,項目名稱:MongoSyphon,代碼行數:12,代碼來源:MongoBulkWriter.java

示例11: getScanStats

import com.mongodb.MongoClient; //導入方法依賴的package包/類
@Override
public ScanStats getScanStats() {
  try{
    MongoClient client = storagePlugin.getClient();
    MongoDatabase db = client.getDatabase(scanSpec.getDbName());
    MongoCollection<Document> collection = db.getCollection(scanSpec
        .getCollectionName());
    String json = collection.find().first().toJson();
    float approxDiskCost = json.getBytes().length * collection.count();
    return new ScanStats(GroupScanProperty.EXACT_ROW_COUNT,
        collection.count(), 1, approxDiskCost);
  } catch (Exception e) {
    throw new DrillRuntimeException(e.getMessage(), e);
  }
}
 
開發者ID:skhalifa,項目名稱:QDrill,代碼行數:16,代碼來源:MongoGroupScan.java

示例12: populateDatabase

import com.mongodb.MongoClient; //導入方法依賴的package包/類
public void populateDatabase(String[][] cellValues, String uploadID){
    MongoClient mongoClient = new MongoClient();
    MongoDatabase test = mongoClient.getDatabase(databaseName);
    MongoCollection plants = test.getCollection("plants");

    String[] keys = getKeys(cellValues);

    for (int i = 4; i < cellValues.length; i++){
        Document doc = new Document();
        for(int j = 0; j < cellValues[i].length; j++){
            doc.append(keys[j], cellValues[i][j]);
        }

        if(doc.get("gardenLocation").equals(""))
            continue;

        // Initialize the empty metadata
        Document metadataDoc = new Document();
        metadataDoc.append("visits", new BsonArray());
        metadataDoc.append("ratings", new BsonArray());
        metadataDoc.append("comments", new BsonArray());

        doc.append("metadata", metadataDoc);
        doc.append("uploadID", uploadID);
        doc.append("photoLocation","");

        plants.insertOne(doc);
    }




    setLiveUploadID(uploadID);
}
 
開發者ID:UMM-CSci-3601-S17,項目名稱:digital-display-garden-iteration-4-dorfner-v2,代碼行數:35,代碼來源:ExcelParser.java

示例13: testUploadFile

import com.mongodb.MongoClient; //導入方法依賴的package包/類
@Test
public void testUploadFile() {
    String filePath = "/IDPH_STD_Illinois_By_County_By_Sex.xlsx";
    Object object = new Object();
    InputStream excelFile = object.getClass().getResourceAsStream(filePath);

    flowerController.uploadFile(excelFile);

    MongoClient mongoClient = new MongoClient();
    MongoDatabase ddg = mongoClient.getDatabase("ddg");
    MongoCollection flowers = ddg.getCollection("flowers");
    assertEquals(1664, flowers.count());
}
 
開發者ID:UMM-CSci-3601-S17,項目名稱:digital-display-garden-iteration-2-spraguesanborn,代碼行數:14,代碼來源:FlowerControllerSpec.java

示例14: MongoMain

import com.mongodb.MongoClient; //導入方法依賴的package包/類
MongoMain() {
    mongo = new MongoClient("localhost", 27017);
    mongoDatabase = mongo.getDatabase("testdb"); //create if not exist
}
 
開發者ID:vitaly-chibrikov,項目名稱:otus_java_2017_04,代碼行數:5,代碼來源:MongoMain.java

示例15: SimpleMongodbAccessor

import com.mongodb.MongoClient; //導入方法依賴的package包/類
public SimpleMongodbAccessor(String ip, int port, String databaseName) {
    mongoClient = new MongoClient(ip, port);
    mongoDatabase = mongoClient.getDatabase(databaseName);
}
 
開發者ID:wxz1211,項目名稱:dooo,代碼行數:5,代碼來源:SimpleMongodbAccessor.java


注:本文中的com.mongodb.MongoClient.getDatabase方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。