mongo-java-driver

Spring mongo queries set custom timeout

这一生的挚爱 提交于 2019-12-01 23:59:57
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).connectTimeout(300).maxWaitTime(300); return new SimpleMongoDbFactory(new MongoClientURI(mongoUri, options)

How to filter documents based on an embedded array?

人盡茶涼 提交于 2019-12-01 10:47:32
After reviewing this page , specifically this query db.scores.find( { results: { $elemMatch: { $gte: 80, $lt: 85 } } } ) I used the following imports import static com.mongodb.client.model.Filters.and; import static com.mongodb.client.model.Filters.elemMatch; import static com.mongodb.client.model.Filters.eq; import static com.mongodb.client.model.Projections.excludeId; import static com.mongodb.client.model.Projections.fields; import static com.mongodb.client.model.Projections.include; And came up with the following code to perform a similar operation ( ARRAY_FIELD_NAME = "myArray" )

Bulk Upsert with MongoDB Java 3.0 Driver

可紊 提交于 2019-11-30 12:13:54
问题 In the earlier versions of MongoDB Java drivers , to run a query and do unordered bulk upsert on the result all we had do was : BulkWriteOperation bulk = dbCollection.initializeUnorderedBulkOperation(); bulk.find(searchQuery).upsert().update(new BasicDBObject("$set", getDbObjectModel())); But in version 3, with the introduction of Bson Document support and MongoCollection.bulkWrite() method how can this be done? I tried this : List<WriteModel<Document>> documentList = new ArrayList<>();

How can I build an $or query for MongoDB using the Java driver?

血红的双手。 提交于 2019-11-28 05:17:32
I'm trying to Or some conditions in MongoDB (using the Java driver). This is what I'm doing : Pattern regex = Pattern.compile("title"); DBCollection coll = MongoDBUtil.getDB().getCollection("post_details"); BasicDBObject query = new BasicDBObject(); query.put("category_title", "myCategory"); query.append("post_title", regex); query.append("post_description", regex); DBCursor cur = coll.find(query); while(cur.hasNext()) { System.out.println(cur.next().get("post_id")); } I'd like to use the $or operand on these conditions, but I guess the default is "and" and I don't know how to change it. In

How to apply update using Filtered positional operator with arrayFilters

狂风中的少年 提交于 2019-11-27 19:37:47
问题 I am running on Mongodb 3.6, with mongo driver 3.4.3 and spring data mongo 1.5.10. Below is the structure of my document { "_id": 12345, "_class": "com.example.ProductRates", "rates": [ { "productId": NumberInt(1234), "rate": 100.0, "rateCardId": NumberInt(1), "month": NumberInt(201801) }, { "productId": NumberInt(1234), "rate": 200.0, "rateCardId": NumberInt(1), "month": NumberInt(201802) }, { "productId": NumberInt(1234), "rate": 400.0, "rateCardId": NumberInt(2), "month": NumberInt(201803)

Mongo DB Java 3.x Driver - Group By Query

ⅰ亾dé卋堺 提交于 2019-11-27 08:27:00
问题 I'd like to learn how to implement group by query using Mongo DB Java 3.x Driver . I want to group my collection through the usernames , and sort the results by the count of results DESC . Here is the shell query which I want to implement the Java equivalent: db.stream.aggregate({ $group: {_id: '$username', tweetCount: {$sum: 1} } }, { $sort: {tweetCount: -1} } ); Here is the Java code that I have implemented: BasicDBObject groupFields = new BasicDBObject("_id", "username"); // count the

How can I build an $or query for MongoDB using the Java driver?

时间秒杀一切 提交于 2019-11-27 00:42:09
问题 I'm trying to Or some conditions in MongoDB (using the Java driver). This is what I'm doing : Pattern regex = Pattern.compile("title"); DBCollection coll = MongoDBUtil.getDB().getCollection("post_details"); BasicDBObject query = new BasicDBObject(); query.put("category_title", "myCategory"); query.append("post_title", regex); query.append("post_description", regex); DBCursor cur = coll.find(query); while(cur.hasNext()) { System.out.println(cur.next().get("post_id")); } I'd like to use the $or