mongodb-java

PersistenceConstructor argument variable name doesn't match instance variable name

时间秒杀一切 提交于 2019-12-01 03:58:41
问题 I'm trying to persist the following object with spring-data-mongodb version 1.1.1.RELEASE : @Document public static class TestObject { private final int m_property; @PersistenceConstructor public TestObject(int a_property) { m_property = a_property; } public int property() { return m_property; } } I get a MappingException when I try to read the object back from the database (see full stacktrace below) The naming convention my group uses requires argument variable names to be prefaced by a_

resolving java result 137

谁说我不能喝 提交于 2019-12-01 03:17:53
I am running a java process on amazon ec2. It ran for 72 mins and then suddenly I get "java result 137". That is all, there are no exceptions or any other error messages. I have searched for this error but couldn't find anything useful. What could be the cause of it and how to resolve it? Please let me know. centic Exit codes above 127 typically mean the process was stopped because of a Signal . The exit code 137 then resolves to 128 + 9, whereas Signal 9 is SIGKILL, i.e. the process was forcefully killed. This can among others be a "kill -9 " command. However in your case this could be an out

How to delete all documents in mongodb collection in java

霸气de小男生 提交于 2019-11-30 22:06:57
问题 I want to delete all documents in a collection in java. Here is my code: MongoClient client = new MongoClient("10.0.2.113" , 27017); MongoDatabase db = client.getDatabase("maindb"); db.getCollection("mainCollection").deleteMany(new Document()); Is this the correct way to do this? I am using MongoDB 3.0.2 回答1: To remove all documents use the BasicDBObject or DBCursor as follows: MongoClient client = new MongoClient("10.0.2.113" , 27017); MongoDatabase db = client.getDatabase("maindb");

How to retrieve a single field from MongoDB in Java

穿精又带淫゛_ 提交于 2019-11-30 20:06:26
问题 I am trying to retrieve a field from a document and set it equal to a variable so when I update it later I can just do (field + newCount) or something like that. For example my document is: { "username":"testusername" "item":"Sofa" "price":150.0 } How do I retrieve the price field and set it equal to let's say double currentPrice; ? The documentation is a bit ambiguous so an example would help. Also, I am using mongoDB Java version 3.0.2 Thanks EDIT: Apparently the question is a bit ambiguous

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);

resolving java result 137

纵然是瞬间 提交于 2019-11-30 16:55:26
问题 I am running a java process on amazon ec2. It ran for 72 mins and then suddenly I get "java result 137". That is all, there are no exceptions or any other error messages. I have searched for this error but couldn't find anything useful. What could be the cause of it and how to resolve it? Please let me know. 回答1: Exit codes above 127 typically mean the process was stopped because of a Signal. The exit code 137 then resolves to 128 + 9, whereas Signal 9 is SIGKILL, i.e. the process was

How to import/export all collections of the MongoDB database using mongodb java driver?

一世执手 提交于 2019-11-30 09:33:52
问题 Is there any function to import and export all collections of mongodb database using java driver.? like there is mongodump and mongorestore using command prompt. 回答1: The short answer is no. These commands can be invoked only from command line. You might consider to fetch all the data from all collections but its expected to be slow. You can read the discussion around this here Hope this helps 回答2: (same answer as here) recently I've started a project called mongodbdump-java-wrapper to wrap

SocketTimeout with opened connection in MongoDB

北战南征 提交于 2019-11-30 07:21:25
I've a Java application that performs some aggregations on MongoDB, but sometimes it just hangs and throw a SocketTimeout exception. After the Exception the app will run just fine (for a bit, then it will probably raise again the exception). I've just found this explanation that seems a possible cause but I'm not sure. I initalize the MongoClient and keep the connection to the DB open. I'm not sure if this could be a problem and I should just get everytime the database and then let the database to be garbage collected (and close the connection). Another approach could be ping periodically

MongoDB Java driver: autoConnectRetry

家住魔仙堡 提交于 2019-11-30 07:19:30
Our current connection configuration looks like this: MongoClientOptions.builder() .autoConnectRetry(true).maxAutoConnectRetryTime(1200000) .socketTimeout(30000).connectTimeout(15000).build(); // SocketTimeout: 30s, ConnectionTimeout 15s, ReconnectRetry: 20min autoConnectRetry and maxAutoConnectRetryTime are deprecated in the current version ( source code ) and will be removed: "There is no replacement for this method. Use the connectTimeout property to control connection timeout." I thought retries and connection timeouts were two different things. Does anyone know why this was changed and

MongoDB update using Java 3 driver

喜你入骨 提交于 2019-11-30 06:40:00
I'm switching to the MongoDB Java driver version 3. I cannot figure out how to perform an update of a Document. For example, I want to change the "age" of an user: MongoDatabase db = mongoClient.getDatabase("exampledb"); MongoCollection<org.bson.Document> coll = db.getCollection("collusers"); Document doc1 = new Document("name", "frank").append("age", 55) .append("phone", "123-456-789"); Document doc2 = new Document("name", "frank").append("age", 33) .append("phone", "123-456-789"); coll.updateOne(doc1, doc2); The output is: java.lang.IllegalArgumentException: Invalid BSON field name name Any