mongodb-java-3.3.0

What is the correct method to update a document while using Mongo Cursor?

守給你的承諾、 提交于 2020-05-15 05:12:00
问题 I am trying to update a document in Mongo DB using cursor. My Mongo DB Java driver version is 3.3.0. Following is my code snippet. MongoCollection<Document> collection = mongoDb.getCollection("customer"); MongoCursor<Document> cursor = collection.find().iterator(); try{ while(cursor.hasNext()){ Document oldDoc = cursor.next(); //created new Document newDoc collection.replaceOne(oldDoc, newDoc); } }catch(Exception e){ e.printStackTrace(); } Though this way, I can update the document, I think

What is the correct method to update a document while using Mongo Cursor?

丶灬走出姿态 提交于 2020-05-15 05:10:50
问题 I am trying to update a document in Mongo DB using cursor. My Mongo DB Java driver version is 3.3.0. Following is my code snippet. MongoCollection<Document> collection = mongoDb.getCollection("customer"); MongoCursor<Document> cursor = collection.find().iterator(); try{ while(cursor.hasNext()){ Document oldDoc = cursor.next(); //created new Document newDoc collection.replaceOne(oldDoc, newDoc); } }catch(Exception e){ e.printStackTrace(); } Though this way, I can update the document, I think

Finding distinct from collections in mongodb

社会主义新天地 提交于 2019-12-20 02:35:19
问题 Our previous implementation for finding distinct elements from a collection used to be : List<String> names = mongoClient.getDB(dbName).getCollection(collectionName).distinct(NAME_KEY); Trying to upgrade this into the current implementation with mongo 3.3.0+ as tried is : List<String> names = mongoClient.getDatabase(dbName) .getCollection(collectionName, TDocType.class) .distinct(NAME_KEY, String.class); // compile error - required Class<TResult> Have also given a try to .distinct(NAME_KEY,

How to read date (Timestamp) from MongoDB using Java

微笑、不失礼 提交于 2019-12-12 03:53:24
问题 Am trying to read date field from MongoDB in below format Formate: YYYY-MM-dd HH:mm:ss.SSSSSS 2017-01-23-10.46.07.812000 - DB2 2017-01-23T16:46:07.812Z - Stored in MongoDB (While viewing from GUI tool) Mon Jan 23 22:16:07 IST 2017 - Result/Reading from MongoDB // Formatter for the input date final DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy"); final ZonedDateTime dateFiledParsed = ZonedDateTime.parse(dateFiled.toString(), inputFormat); final

DBCollection save() equivalent in mongo 3.3

本小妞迷上赌 提交于 2019-12-11 05:29:19
问题 Our current implementation with mongo-java-driver:3.0.4 for a document update is as follows - private void updateParam(String param1, String param2) { Param param = new Param(param1, param2); DBCollection collection = mongoClient.getDB("databaseName").getCollection("collectionName"); collection.save(new BasicDBObject(param.toMap())); } where param.toMap() is implemented as public Map<String, Object> toMap() { return JSONObjectMapper.getObjectMapper().convertValue(this, Map.class); } With