mongo-java-driver


Parse sql query using antlr parsetree to mongo bson document in Java

纵然是瞬间 提交于 2020-01-14 05:37:11
问题 I have a SQL like query example: Select id,name from employee where age > 30 and department = 'IT' limit 200 The SQL query grammer is defined in an ANTLR4 grammar file. Is there any implementation that converts the parse tree of this query to a bson document? The bson document will then be used to query a mongo db. 回答1: In one of my previous jobs I did something similar: got a query (not an sql, but pretty similar) and translated it to mongo query with antlr. I don't have a code to share,

How to write redact aggregation in java?

淺唱寂寞╮ 提交于 2020-01-06 21:13:23
问题 I'm trying to convert MongoDb query using aggregation framework using Java driver. I have been helped to create query here How to apply filter for output of aggregation framework of Mongo Db?. Here is the sample aggregate query: db.movies.aggregate( [{ $redact: { $cond: { if: {$gt: [{ $avg: "$customerReviews"}, 7 ] }, then: "$$KEEP", else: "$$PRUNE" } } }, {$skip:1}, {$limit:2} ] ); I started with: BasicDBObject avgQuery = new BasicDBObject("$avg", "$customerReviews"); But cannot figure out

How to create Decimal128 field with inc operator in java/scala

笑着哭i 提交于 2020-01-04 05:40:08
问题 I have following document structure: { "moneys": { "someKey": NumberDecimal(99) ... "someOtherRandomKey": NumberDecimal(99) } { What I want : When nonexistent field increments, create that field with NumberDecimal value. I tried it with scala driver but cant do that: //not compiles collection.findOneAndUpdate(filters,Updates.inc("someOtherKey", new Decimal128(50))) because Updates.inc(k,v) requires Number ; Decimal128 is not Number I think problem not in driver, but with my logic. How can I

java code gives error in mongodb while compiling

给你一囗甜甜゛ 提交于 2020-01-02 07:10:13
问题 I am new to mongodb and i have the following code import com.mongodb.*; import com.mongodb.Block; import com.mongodb.client.AggregateIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; import org.bson.types.ObjectId; import static java.util.Arrays.asList; public class getAssets{ public static void main( String args[] ){ Block<Document> printBlock = new Block<Document>() { @Override public void apply(final Document document) {

Bson pretty print using Java MongoDb driver

我怕爱的太早我们不能终老 提交于 2019-12-30 07:22:09
问题 I am using the Mongo Aggregation Framework using the Java MongoDB driver, version 3.3. I have an aggregagtion pipeline , that is merely collection of type List<Bson> . I am trying to find a way to pretty print each stage of the pipeline. Calling the toString method on each element is not sufficient, because each stages is an instance of a simple implementation of the Bson interface, which is SimplePipelineStage . This stupid class has not any override of the toString method. The pipeline is

MongoDB Scala - query document for a specific field value

。_饼干妹妹 提交于 2019-12-23 03:29:06
问题 So I know that in Mongo Shell, you use dot notation to get the field you want in any document. How is dot notation achieved in MongoDB Scala. I'm confused as to how it works. Here is the code that fetches a document from a collection: val record = collection.find().projection(fields(include("offset"), excludeId())).limit(1) EDIT: I'm trying to work on a mechanism to basically re-consume Kafka records at a point where the consumer was shutdown. To do this, I store my kafka records in an

MongoDB BasicDBObject vs Document in java

╄→гoц情女王★ 提交于 2019-12-21 12:22:29
问题 I am using MongoDB v3.2.0 with Mongo Java Driver 3.0.4 version. I am using the BasicDBObject (deprecated) instead of using the Document in java, as I need to do many changes for converting to Document in my standalone java project. Can anyone tell me changing into Document, will there be any performance improvement in memory and large collection inserts and reads. Is there any way to improve my frequent write and read operations on MongoDB using java. 回答1: Basic DBobject is not deprecated .

Spring mongo queries set custom timeout

↘锁芯ラ 提交于 2019-12-20 02:43:13
问题 I would like to lower the timeout setting in my spring-mongo java application (the query should fail after 300 ms if the database is not accessible). I tried this config: @Configuration public class MongoConfiguration { private String mongoUri = "mongodb://127.0.0.1:27017/myDb?connectTimeoutMS=300&socketTimeoutMS=300&waitQueueTimeoutMS=300&wtimeoutMS=300"; @Bean public MongoDbFactory mongoDbFactory() throws Exception { Builder options = new MongoClientOptions.Builder().socketTimeout(300)

How to properly handle exceptions in MongoClient for VertX

走远了吗. 提交于 2019-12-11 17:19:54
问题 In the startup method of my application I want to check that the credentials for MongoDB provided to the application are OK. If they are OK, I continue the startup, if not, the application is supposed to exit as it cannot connect to the DB. The code snippet is as below: // Create the client MongoClient mongodb = null; try { mongodb = MongoClient.createShared(vertx, mongo_cnf, mongo_cnf.getString("pool_name")); } catch(Exception e) { log.error("Unable to create MongoDB client. Cause: '{}'.

Inserting a POJO with a JsonNode field into MongoDB using Java driver

这一生的挚爱 提交于 2019-12-11 14:46:58
问题 Im using Mongo Java driver 3.7 This is my POJO(with getters and setters) - public class Sample{ public int field1; public JsonNode field2; } Im using the below code to insert an object of Sample into MongoDB. MongoCollection<Sample> myCollection = database.getCollection("myCollection",Sample.class); ObjectMapper mapper = new ObjectMapper(); Sample obj = new Sample(); obj.setField1(1); String sampleJSON = "{ \"key\": \"value\" }"; obj.setField2(mapper.readTree(sample)); myCollection.insertOne

工具导航Map