MongoSocketReadException: Prematurely reached end of stream (after a period of inactivity)

I found it in some documentation: For long running applications, it is often prudent to enable “keepAlive” with a number of milliseconds. Without it, after some period of time you may start to see “connection closed” errors for what seems like no reason. Check if this helps. When you connect to mongoDB you can pass … Read more

How to insert multiple documents at once in MongoDB through Java

DBCollection.insert accepts a parameter of type DBObject, List<DBObject> or an array of DBObjects for inserting multiple documents at once. You are passing in a string array. You must manually populate documents(DBObjects), insert them to a List<DBObject> or an array of DBObjects and eventually insert them. DBObject document1 = new BasicDBObject(); document1.put(“name”, “Kiran”); document1.put(“age”, 20); DBObject … Read more

Mockito.any() for

Note that documentToPojo takes a Class as its second argument. any(Foo.class) returns an argument of type Foo, not of type Class<Foo>, whereas eq(WorkItemDTO.class) should return a Class<WorkItemDTO> as expected. I’d do it this way: when(mongoUtil.documentToPojo( Mockito.any(Document.class), Mockito.eq(WorkItemDTO.class))).thenReturn(…);

Why do I end up with java.lang.IllegalArgumentException for Casbah / Java MongoDB Driver?

The version of the Java driver you’re using is not compatible with MongoDB 2.2. You should be using at least 2.9.3, if not 2.10 or higher. There’s no guarantee a driver upgrade will fix your problem, but it should be a first step. https://support.mongolab.com/entries/22631012-which-drivers-support-mongodb-2-2

How to get mongo command results in to a flat file

you can try the following from the command line mongo 127.0.0.1/db –eval “var c = db.collection.find(); while(c.hasNext()) {printjson(c.next())}” >> test.txt assuming you have a database called ‘db’ running on localhost and a collection called ‘collection’ this will export all records into a file called test.txt If you have a longer script that you want to … Read more

Difference between createIndex() and ensureIndex() in java using mongodb

Update 2: The original answer, as well as the first update, erroneously reference the Mongo shell documentation instead of the Java API. In Java, DBCollection.ensureIndex() was deprecated in version 2.12 and removed in version 3.0. DBCollection.createIndex() is the one that should be used. Update: db.collection.ensureIndex() is deprecated since version 3.0.0. Is now an alias for … Read more

Spring Data MongoDB Date Between

Do not include the ‘and(“startDate”)‘ part in your criteria. Instead of : query.addCriteria(Criteria.where(“startDate”).gte(startDate).and(“startDate”).lt(endDate)); You should use: query.addCriteria(Criteria.where(“startDate”).gte(startDate).lt(endDate)); When you include the ‘and(“startDate”)’ part, Mongo see’s it as two different entries on the same property.

Case insensitive sorting in MongoDB

Update: As of now mongodb have case insensitive indexes: Users.find({}) .collation({locale: “en” }) .sort({name: 1}) .exec() .then(…) shell: db.getCollection(‘users’) .find({}) .collation({‘locale’:’en’}) .sort({‘firstName’:1}) Update: This answer is out of date, 3.4 will have case insensitive indexes. Look to the JIRA for more information https://jira.mongodb.org/browse/SERVER-90 Unfortunately MongoDB does not yet have case insensitive indexes: https://jira.mongodb.org/browse/SERVER-90 and the … Read more

How do I partially update an object in MongoDB so the new object will overlay / merge with the existing one

I solved it with my own function. If you want to update specified field in document you need to address it clearly. Example: { _id : …, some_key: { param1 : “val1”, param2 : “val2”, param3 : “val3” } } If you want to update param2 only, it’s wrong to do: db.collection.update( { _id:…} , … Read more

tech