mongo-java

how to check from a driver, if mongoDB server is running

大兔子大兔子 提交于 2019-11-29 03:19:50
I wonder, if there is a way to check if mongoDB server is running from java driver for mongoDB? According to the tutorial, I can do Mongo m = new Mongo(); // or Mongo m = new Mongo( "localhost" , 27017 ); // and DB db = m.getDB( "mydb" ); But how to check that I can use these Mongo and DB? I see no isConnected() method in the API. db.getConnector().isOpen() returns true The only way I found is call db.getDatabaseNames() and catch MongoException. If there some more civilized approach? Gates VP if there is a way to check if mongoDB server is running from java driver for MongoDB? So if you can do

How to find documents matching multiple criteria

浪尽此生 提交于 2019-11-29 02:33:11
I'm trying to query a collection using operands AND'd together. I've got the shell version working: db.widgets.find({color: 'black, shape: 'round', weight: 100}) I'm unable to find the Java equivalent (using the native driver ). I've tried various things, but here is my latest attempt: // Find all black, round widgets with weight 100 List<BasicDBObject> criteria = new ArrayList<BasicDBObject>(); criteria.add(new BasicDBObject("color", "black")); criteria.add(new BasicDBObject("shape", "round")); criteria.add(new BasicDBObject("weight", 100)); DBCursor cur = widgets.find(new BasicDBObject("$and

Spring data mongodb not closing mongodb connections

别来无恙 提交于 2019-11-28 01:07:06
问题 I am using spring-data-mongodb (1.7.0.RELEASE) with spring-webmvc framework for my web application. I am using basic CRUD functions using mongoRepository but i am not closing mongo connections in my code cause i thought that spring-data-mongodb will close it by itself, But it keeps on opening new connections and not closing them. These too many connections ares crashing my application and i have to restart tomcat again and again (twice a day) to overcome this. Note: Spring Application &

how to execute mongo admin command from java

♀尐吖头ヾ 提交于 2019-11-27 18:16:05
问题 I want to execute soem admin command with parameters from java. The commands are: { enablesharding : "test" } { shardcollection : "test.test_collection", key : {"number":1} } How can I do it from java driver? The following code doesn't works: mongo.getDb("admin").command("{shardcollection : \"test.test_collection\", key:\"number\":1} }") 回答1: I just found it DB db = mongo.getDB("admin"); DBObject cmd = new BasicDBObject(); cmd.put("shardcollection", "testDB.x"); cmd.put("key", new

How to find documents matching multiple criteria

 ̄綄美尐妖づ 提交于 2019-11-27 17:04:42
问题 I'm trying to query a collection using operands AND'd together. I've got the shell version working: db.widgets.find({color: 'black, shape: 'round', weight: 100}) I'm unable to find the Java equivalent (using the native driver). I've tried various things, but here is my latest attempt: // Find all black, round widgets with weight 100 List<BasicDBObject> criteria = new ArrayList<BasicDBObject>(); criteria.add(new BasicDBObject("color", "black")); criteria.add(new BasicDBObject("shape", "round")

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 do I update fields of documents in mongo db using the java driver?

最后都变了- 提交于 2019-11-27 03:12:15
问题 References: http://www.mongodb.org/display/DOCS/Java+Tutorial Still pretty new to mongo db but I'm trying to update part of an existing document inside a collection... unfortunately, the link above doesn't have an update example. Essentially, i just want to be able to: Add new fields to a document Update existing fields of a document to a new value Here's my code (Grails + Groovy + Java + MongoDB + the java driver): def shape = mongo.shapes.findOne(new BasicDBObject("data", "http://www.foo

How to aggregate by year-month-day on a different timezone

心已入冬 提交于 2019-11-27 00:46:25
I have a MongoDB whom store the date objects in UTC. Well, I want to perform aggregation by year,month day in a different timezone (CET). doing this, works fine for UTC: BasicDBObject group_id = new BasicDBObject("_id", new BasicDBObject("year", new BasicDBObject("$year", "$tDate")). append("month", new BasicDBObject("$month", "$tDate")). append("day", new BasicDBObject("$dayOfMonth", "$tDate")). append("customer", "$customer")); BasicDBObject groupFields = group_id. append("eventCnt", new BasicDBObject("$sum", "$eventCnt")); BasicDBObject group = new BasicDBObject("$group", groupFields); or,

(MongoDB Java) $push into array

醉酒当歌 提交于 2019-11-26 09:36:31
问题 I\'m using mongo 2.2.3 and the java driver. My dilemma, I have to $push a field and value into an array, but I cant seem to figure out how to do this. A sample of my data: \"_id\" : 1, \"scores\" : [ { \"type\" : \"homework\", \"score\" : 78.97979 }, { \"type\" : \"homework\", \"score\" : 6.99 }, { \"type\" : \"quiz\", \"score\" : 99 } ] I can $push in the shell: db.collection.update({_id:1},{$push:{scores:{type:\"quiz\", score:99}}}) but it\'s when I translate this into java I confuse my