mongodb-java

How to perform Update operations in GridFS (using Java)?

半世苍凉 提交于 2019-11-30 06:01:30
问题 I am using Mongo-Java-Driver 2.13 I stored a PDF file ( size 30mb ) in GridFS . I am able to perform insertion, deletion and find operation easily. MongoClient mongo = new MongoClient("localhost", 27017); DB db = mongo.getDB("testDB"); File pdfFile = new File("/home/dev/abc.pdf"); GridFS gfs = new GridFS(db,"books"); GridFSInputFile inputFile = gfs.createFile(pdfFile); inputFile.setId("101"); inputFile.put("title", "abc"); inputFile.put("author", "xyz"); inputFile.save(); data is persisted in

Spring Data MongoDB Date Between

淺唱寂寞╮ 提交于 2019-11-29 22:56:55
I use spring data mongodb. I want the records between two dates. The following MongoDB Query works: db.posts.find({startDate: {$gte: start, $lt: end}}); My attempted Spring data query object code translation does not work: Query query = new Query(); query.addCriteria(Criteria.where("startDate").gte(startDate) .and("startDate").lt(endDate)); What is the correct order of method calls to build the Mongo query I need? 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

How to catch exception when creating MongoClient instance

非 Y 不嫁゛ 提交于 2019-11-29 15:46:08
I am using Mongo DB java driver to connect to mongo instance. Below is the code I am using to create the MongoClient instance. try { new MongoClient("localhost", 1111); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } If the hostname or port number is not correct, I will get below exceptions. I wander how I can catch that exceptions. MongoDB connecting is happening in an internal thread which can't be catched by client code. I want to know whether the MongoClient has connected correctly or not. How can I get that information? INFO: Exception in monitor thread

Insert document in mongodb with autoincrement field from java

旧巷老猫 提交于 2019-11-29 11:42:53
I am trying to insert a document with a sequence number, in one transaction, from java. Something similar to this: function getNextSequence(name) { var ret = db.counters.findAndModify( { query: { _id: name }, update: { $inc: { seq: 1 } }, new: true } ); return ret.seq; } collection.insert({ number: getNextSequence("userid"), name: "Some Name" }); Is it possible to do this from java? Preferably with the official java driver. Following the documentation for creating an Auto-Incrementing Sequence Field , we adapt it to be used in Java with the Java MongoDB driver . Example implementation: import

Append data to existing gridfs file

邮差的信 提交于 2019-11-29 11:37:37
As i can see java mongo driver does not provide capability to get OutputStream from existing gridFS file com.mongodb.gridfs.GridFSFile I have to create GridFSInputFile directly or use gridFs.createFile() method. Is it a lack of java driver or a restriction of gridfs ? Could you suggest any workaround except create new file/delete old one ? Thank you pingw33n GridFS is not a core feature of MongoDB but a convention of storing binary data with accompanying metadata. You should be able to modify any document in fs.chunks collection in a usual manner while keeping corresponding document in fs

MongoDB Java driver: autoConnectRetry

ぐ巨炮叔叔 提交于 2019-11-29 09:25:02
问题 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

SocketTimeout with opened connection in MongoDB

孤人 提交于 2019-11-29 08:29:39
问题 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

MongoDB update using Java 3 driver

拜拜、爱过 提交于 2019-11-29 07:40:32
问题 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

Call stored function in mongodb

好久不见. 提交于 2019-11-29 02:32:49
I'm getting some difficulties to call a stored function within mongdb. I'm a little bit newbie with mongo. [EDIT] : my function stored in mongodb function() { var cndTime = new Date().getTime() - (3600*1000*24*2); // condition db.urls.find(needParse: false).forEach(function(item){ if(item.date < cndTime) // check db.urls.update({_id: item._id}, {$set: { needParse: true }}); // update field }); } All I'm asking is how to invoke this function using reactivemongo or native API. Consider the following example from the mongo shell that first saves a function named echoFunction to the system.js

MongoDb connection refused

大憨熊 提交于 2019-11-29 01:19:40
This is my first attempt to consume MongoDB. I've got Mongo running: ps -ef | grep [m]ongo mongodb 11023 1 0 Jun24 ? 00:00:03 /usr/lib/mongodb/mongod --config /etc/mongodb.conf And the error comes as the result of doing Datastore.save( stuff ); // (pseudo code) The error: Jun 27, 2011 3:20:29 PM com.mongodb.DBTCPConnector fetchMaxBsonObjectSize WARNING: Exception determining maxBSON size using0 java.io.IOException: couldn't connect to [russ-elite-book/127.0.1.1:27017] bc:java.net.ConnectException: Connection refused at com.mongodb.DBPort._open(DBPort.java:206) at com.mongodb.DBPort.go(DBPort