use of com.mongodb.client.MongoDatabase in project graylog2-server by Graylog2.
the class V20170110150100_FixAlertConditionsMigrationTest method setUp.
@Before
public void setUp() throws Exception {
this.clusterConfigService = spy(new ClusterConfigServiceImpl(objectMapperProvider, fongoRule.getConnection(), nodeId, new ChainingClassLoader(getClass().getClassLoader()), new ClusterEventBus()));
final MongoConnection mongoConnection = spy(fongoRule.getConnection());
final MongoDatabase mongoDatabase = spy(fongoRule.getDatabase());
when(mongoConnection.getMongoDatabase()).thenReturn(mongoDatabase);
this.collection = spy(mongoDatabase.getCollection("streams"));
when(mongoDatabase.getCollection("streams")).thenReturn(collection);
this.migration = new V20170110150100_FixAlertConditionsMigration(mongoConnection, clusterConfigService);
}
use of com.mongodb.client.MongoDatabase in project yyl_example by Relucent.
the class MongoDbJDbcTest method main.
public static void main(String[] args) {
try (MongoClient client = new MongoClient("localhost", 27017)) {
MongoDatabase database = client.getDatabase("my-test-db");
//获取集合
MongoCollection<Document> collection = database.getCollection("test-collection");
//删除集合
collection.drop();
//插入文档
for (int i = 0; i < 3; i++) {
Document document = new Document();
document.put("_id", i);
document.put("name", "测试" + i);
collection.insertOne(document);
}
//更新文档
{
Document document = new Document();
document.put("name", "测试零");
collection.updateMany(Filters.eq("_id", 0), new Document("$set", document));
}
System.out.println(collection.count());
//删除文档
collection.deleteOne(Filters.eq("_id", 2));
//检索文档
FindIterable<Document> findIterable = collection.find();
MongoCursor<Document> mongoCursor = findIterable.iterator();
while (mongoCursor.hasNext()) {
System.out.println(mongoCursor.next());
}
System.out.println(collection.count());
//删除库
database.drop();
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
}
use of com.mongodb.client.MongoDatabase in project drill by apache.
the class MongoPersistentStoreProvider method start.
@Override
public void start() throws IOException {
MongoClientURI clientURI = new MongoClientURI(mongoURL);
client = new MongoClient(clientURI);
MongoDatabase db = client.getDatabase(clientURI.getDatabase());
collection = db.getCollection(clientURI.getCollection()).withWriteConcern(WriteConcern.JOURNALED);
Bson index = Indexes.ascending(pKey);
collection.createIndex(index);
}
use of com.mongodb.client.MongoDatabase in project drill by apache.
the class MongoGroupScan method getScanStats.
@Override
public ScanStats getScanStats() {
try {
MongoClient client = storagePlugin.getClient();
MongoDatabase db = client.getDatabase(scanSpec.getDbName());
MongoCollection<Document> collection = db.getCollection(scanSpec.getCollectionName());
long numDocs = collection.count();
float approxDiskCost = 0;
if (numDocs != 0) {
//toJson should use client's codec, otherwise toJson could fail on
// some types not known to DocumentCodec, e.g. DBRef.
final DocumentCodec codec = new DocumentCodec(client.getMongoClientOptions().getCodecRegistry(), new BsonTypeClassMap());
String json = collection.find().first().toJson(codec);
approxDiskCost = json.getBytes().length * numDocs;
}
return new ScanStats(GroupScanProperty.EXACT_ROW_COUNT, numDocs, 1, approxDiskCost);
} catch (Exception e) {
throw new DrillRuntimeException(e.getMessage(), e);
}
}
Aggregations