mongo-java

Use a single MongoClient across a JavaEE web service

陌路散爱 提交于 2019-12-01 01:16:46
问题 After reading the mongo documentation that says each instance of a MongoClient handles its own pooling, how would I go about only having one instance across my whole application? This seems like it could be a scenario for using a singleton bean, but this seems like it would defeat the purpose of connection pooling. If only one user would be able to access the bean that contains the MongoClient instance at a time, surely multiple connections in the pool would never be used at the same time.

how to serialize class?

。_饼干妹妹 提交于 2019-11-30 23:51:32
问题 When I insert a List into mongodb, there is a problem: Exception in thread "main" java.lang.IllegalArgumentException: can't serialize class mongodb.Person at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:234) at org.bson.BasicBSONEncoder.putIterable(BasicBSONEncoder.java:259) at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:198) at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:140) at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:86)

How to execute full text search command in MongoDB with Java Driver ?

人走茶凉 提交于 2019-11-30 20:23:59
Mongo and Java gurus. Our team decided to use full text search API, introduced recently in MongoDB. However, we found some difficulties executing the command using the Java MongoDB driver. here is my code I am using : public BasicDBObject find(String search) { BasicDBObject searchCommand = new BasicDBObject(); searchCommand.put("text", new BasicDBObject().append("search", search)); CommandResult commandResult = db.command(searchCommand); } This is what I get when I print System.out.println(commandResult) { "serverUsed" : "/127.0.0.1:27017" , "errmsg" : "exception: wrong type for field (text) 3

how to prevent logging on console when connected to mongodb from java?

早过忘川 提交于 2019-11-30 18:27:42
Hi i am trying to learn mongo-java driver.i followed this mongodb documentation . The below is my code public class JMongoDBCDemo { MongoClient mongoClient; DB db; DBCollection coll; public JMongoDBCDemo() { MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); db = mongoClient.getDB( "messenger" ); coll = db.getCollection("users"); DBObject myDoc = coll.findOne(); System.out.println(myDoc); mongoClient.close(); System.out.println("Got a collection..."); } public static void main(String[] args){ JMongoDBCDemo mongoDemo = new JMongoDBCDemo(); } } the below is the output Apr 05, 2015

Difference between cursor.count() and cursor.size() in MongoDB

99封情书 提交于 2019-11-30 17:25:10
What is the difference between the cursor.count() and cursor.size() methods of MongoDB's DBCursor ? Parvin Gasimzade From the Javadoc of the MongoDB Java Driver , it says : DBCursor.count() : Counts the number of objects matching the query. This does not take limit/skip into consideration. DBCursor.size() : Counts the number of objects matching the query. This does take limit/skip into consideration. David Chaverri More than an answer I'd like to point out an issue that our team faced "mixing" this two. We had something like this: DBCursor cursor = collection.find(query).limit(batchSize);

$push and $set in same MongoDB update

雨燕双飞 提交于 2019-11-30 06:15:40
I'm trying to use MongoDB's Java driver to make two updates ($set and $push) to a record in the same operation. I'm using code similar to the following: BasicDBObject pushUpdate = new BasicDBObject().append("$push", new BasicDBObject().append("values", dboVital)); BasicDBObject setUpdate = new BasicDBObject().append("$set", new BasicDBObject().append("endTime", time)); BasicDBList combinedUpdate = new BasicDBList(); combinedUpdate.add( pushUpdate); combinedUpdate.add( setUpdate); collection.update( new BasicDBObject().append("_id", pageId), combinedUpdate, true, false); When I combine the $set

Executing Mongo like Query (JSON)through Java

浪尽此生 提交于 2019-11-30 01:52:43
I was wondering if there is a way to execute mongo like query directly through Java i.e. we give mongoDB like query as a String to a function in Java driver for mongoDB as a String Object and an DBCursor Object is returned. Something like: import com.mongodb.*; import java.net.UnknownHostException; public class ExecuteQuery { public static void main(String args[]){ try{ Mongo m = new Mongo(); DB db = m.getDB("test"); DBCollection coll = db.getCollection("first"); DBObject doc = new BasicDBObject(); DBCursor cur =coll.executeQuery("db.first.find({"username":"joe"})"); } catch

how to prevent logging on console when connected to mongodb from java?

☆樱花仙子☆ 提交于 2019-11-30 01:42:30
问题 Hi i am trying to learn mongo-java driver.i followed this mongodb documentation. The below is my code public class JMongoDBCDemo { MongoClient mongoClient; DB db; DBCollection coll; public JMongoDBCDemo() { MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); db = mongoClient.getDB( "messenger" ); coll = db.getCollection("users"); DBObject myDoc = coll.findOne(); System.out.println(myDoc); mongoClient.close(); System.out.println("Got a collection..."); } public static void main

Spring data mongodb not closing mongodb connections

若如初见. 提交于 2019-11-29 07:26:28
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 & mongod is on same server. This is log just after crashing - 2015-07-17T01:31:20.068-0400 I NETWORK [conn3645

$push and $set in same MongoDB update

梦想的初衷 提交于 2019-11-29 05:09:10
问题 I'm trying to use MongoDB's Java driver to make two updates ($set and $push) to a record in the same operation. I'm using code similar to the following: BasicDBObject pushUpdate = new BasicDBObject().append("$push", new BasicDBObject().append("values", dboVital)); BasicDBObject setUpdate = new BasicDBObject().append("$set", new BasicDBObject().append("endTime", time)); BasicDBList combinedUpdate = new BasicDBList(); combinedUpdate.add( pushUpdate); combinedUpdate.add( setUpdate); collection