mongo-java

Mongo opens too many connections

随声附和 提交于 2019-12-12 07:47:58
问题 I am trying to write a lot of data to MongoDB, in a Java loop. I am getting errors based on the number of connections open. My theory is that since MongoDB is not transactional, lots of connections can be opened simultaneously. However the Java code is also able to loop very fast, after a certain time the number of loop iterations starts overtaking the number of available connections and Mongo hits a wall. My code looks like this. I've seen it recommended to not do m.close() but then you just

How to define “$add :[“$count” , “$count1”]” aggregation with mongo-java implementation?

假装没事ソ 提交于 2019-12-12 03:42:12
问题 Below is sample document structure. db.volume_statistics.insert({ "client": "SER", "message": "Sample_v02RQ", "GDS": "SABRE", "daily": 240000, "hourly": {"0": 1000, "1": 100, "2": 454, "3":3434, "4":3434, "5":343, ... , ”23”:454 }, "date":ISODate("2013-06-23T04:00:00Z") }); I am trying to write a query to group the information of hourly column by 8 hours{Sum of all 8 hours} and month as per below output format. { "Month" : 5 , "01-08" : 26970, "09-17" : 45970} { "Month" : 6 , "01-08" : 269712

Passing more than one $Unwind object from java driver

旧时模样 提交于 2019-12-12 01:28:16
问题 The mongo java driver takes var args for aggregate method, I have an API in which $unwind objects get's created dynamically and its number is not fixed. how can I pass it through Mongo Java driver aggregate method, as it needs each object to be passed separately. I tried passing putting all the $unwind object in a BasicDBList and pass, but it fails. Can someone help me with some work around? example: db.foo.aggregate({$unwind:items},{$unwind:item2}) , but these unwind may vary as it is

What is wrong with MongoDB remove query using mongodb java for sub-array of document?

人走茶凉 提交于 2019-12-11 23:20:48
问题 Remove in momgodb using java not giving proper result. Am i missing something? My database is as below. My code for removeing records where index = "7" is as, BasicDBObject whereQuery = new BasicDBObject("nodes.index", new BasicDBObject("$eq", "7")); node_info.remove(whereQuery); It's returning all the records and deleting my complete database. What can be probably wrong.? 回答1: Instead of remove try $pull method code as below BasicDBObject match = new BasicDBObject("_id", object id here);

Can't connect MongoDb via JNDI in java

我的未来我决定 提交于 2019-12-11 11:57:43
问题 I am trying to connect MongoDB through JNDI in wildfly using the below code. Context ctx = new InitialContext(); MongoClient mongoClient = (MongoClient) ctx.lookup("java:global/MyMongoClient"); But i am getting the following error. Exception->com.mongodb.MongoClient cannot be cast to com.mongodb.MongoClient I am using mongo-java-driver-3.4.0.jar in "wildfly-10.1.0.Final/modules/system/layers/base/org/mongodb/main" directory and "mongodb-driver-3.4.0.jar" in "wildfly-10.1.0.Final/standalone

group by date in mogodb query without considering time

十年热恋 提交于 2019-12-11 10:06:07
问题 I have the following code: DBObject groupFields = new BasicDBObject("_id", "$Date"); groupFields.put("count", new BasicDBObject("$sum", 1)); DBObject groupBy = new BasicDBObject("$group", groupFields); stages.add(groupBy); DBObject project = new BasicDBObject("_id", 0); project.put("count", 1); project.put("Date", "$_id"); stages.add(new BasicDBObject("$project", project)); DBObject sort = new BasicDBObject("$sort", new BasicDBObject("Date", 1)); stages.add(sort); to group by Date and show

JasperFillManager.fillReport and mongo?

戏子无情 提交于 2019-12-11 07:54:04
问题 I'am currently following a tutorial ( http://kristantohans.wordpress.com/2010/03/01/new-to-jasperreport-build-your-first-impressive-application-part-2/ ) and he use a java db connection (conn) 50 try { 51 //Fill the report with parameter, connection and the stream reader 52 JasperPrint jp = JasperFillManager.fillReport(is, null, conn); How can i do to connect to mongo here ? Because with mongo i have : Mongo m = new Mongo( "localhost" , 27017 ); DB db = m.getDB( "test" ); and JasperPrint jp =

Morphia is there a difference between fetch and asList in performance wise

混江龙づ霸主 提交于 2019-12-11 02:29:40
问题 We are using morphia 0.99 and java driver 2.7.3 I would like to learn is there any difference between fetching records one by one using fetch and retrieving results by asList (assume that there is enough memory to retrieve records through asList ). We iterate over a large collection, while using fetch I sometimes encounter cursor not found exception on the server during the fetch operation, so I need to execute another command to continue, what could be the reason for this? 1-)fetch the

How can I authenticate any database with given username and password in Mongo Java Driver 2.13.0?

天涯浪子 提交于 2019-12-11 02:23:38
问题 Previously I could use db.authenticate(String username, char[] password) method. With 2.13.0, how can I achieve this? 回答1: There is no replacement for db.authenticate(). The driver will use the credentials provided and make sure the connections are authenticated as they are created. Based on this mongodb-user discussion the Java Driver team is open to discussions on what the real need for the db.authenticate(...) method. 回答2: Use import com.mongodb.MongoCredential; MongoCredential mongoCred =

Mongo java driver - retrieve slice of array without any other field

ぐ巨炮叔叔 提交于 2019-12-08 03:10:30
问题 I have a class called user which can be simplified to: class User { String[] friends; //Constructor etc... } It is stored in a mongo collections called users. I am trying to retrieve the first N elements of the friends array without anything else from the class. Right now, I tried using the following java query: db.getCollection("users").find(new BasicDBObject(), new BasicDBObject("friends", new BasicDBObject("$slice", N))).next(); As expected, I get a User object with the friends array slice