mongodb-java

List of all collections in mongo database in java

爱⌒轻易说出口 提交于 2020-06-27 09:08:11
问题 how can I get a list of all the collections in the database? database - mongodb; language - java; ide - eclipse; 回答1: Getting A List Of Collections Each database has zero or more collections. You can retrieve a list of them from the db (and print out any that are there) : Set<String> colls = db.getCollectionNames(); for (String s : colls) { System.out.println(s); } Edit : As suggested in @Andrew's answer, updated java client uses this : /** * Gets the names of all the collections in this

installing MongoDB java driver

你。 提交于 2020-06-25 19:07:29
问题 I'm really new in Java and also MongoDB world. I would like to use MongoDB and Java. I have downloaded MongoDB driver for java. It is a jar file, my question is how can I use this Jar file. I've used this command but I got an error: java -jar mongo-java-driver-2.9.3-javadoc.jar and the error: no main manifest attribute, in mongo-java-driver-2.9.3-javadoc.jar could you please help me to solve the problem? 回答1: First, that is not the correct jar. The jar you are referencing above contains the

installing MongoDB java driver

倖福魔咒の 提交于 2020-06-25 19:07:15
问题 I'm really new in Java and also MongoDB world. I would like to use MongoDB and Java. I have downloaded MongoDB driver for java. It is a jar file, my question is how can I use this Jar file. I've used this command but I got an error: java -jar mongo-java-driver-2.9.3-javadoc.jar and the error: no main manifest attribute, in mongo-java-driver-2.9.3-javadoc.jar could you please help me to solve the problem? 回答1: First, that is not the correct jar. The jar you are referencing above contains the

installing MongoDB java driver

丶灬走出姿态 提交于 2020-06-25 19:06:14
问题 I'm really new in Java and also MongoDB world. I would like to use MongoDB and Java. I have downloaded MongoDB driver for java. It is a jar file, my question is how can I use this Jar file. I've used this command but I got an error: java -jar mongo-java-driver-2.9.3-javadoc.jar and the error: no main manifest attribute, in mongo-java-driver-2.9.3-javadoc.jar could you please help me to solve the problem? 回答1: First, that is not the correct jar. The jar you are referencing above contains the

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

Connection to non existing mongodb server does not throw exception

风格不统一 提交于 2020-04-11 04:46:46
问题 I'm playing around a bit with the MongoDB driver for Java. So I just created a simple application to connect to a MongoDB server and select a database. So I created an instance of MongoClient and selected a 'DB': try { MongoClient client = new MongoClient("localhost", 27017); DB database = client.getDB("example"); }catch(Exception e){ e.printStackTrace(); } Because of the fact that there is no running instance of mongod on my machine, I expected that client would throw an Exception .

MongoDB document update array element using findOneAndUpdate method in Java

穿精又带淫゛_ 提交于 2020-03-18 09:36:06
问题 Can someone look at this issue as we are not able to insert the array in the field 'd' as shown in the image below: We are querying the document to update any field in the ps array as well as insert a array in the d field as shown below. public void updateTesting() { Bson where = new Document().append("_id", 123456789012345L).append("session.ps.apn","abcdef"); Bson update1=new Document().append("th", "value3"); Bson update = new Document() .append("session.ps.$.apn", "klo"); Bson set = new

How to use mongodb $group in java?

狂风中的少年 提交于 2020-02-08 02:33:07
问题 I have a collection processedClickLog in MongoDB. { "_id" : ObjectId("58ffb4cefbe21fa7896e2d73"), "ID" : "81a5d7f48e5df09c9bc006e7cc89d6e6", "USERID" : "206337611536", "DATETIME" : "Fri Mar 31 17:29:34 -0400 2017", "QUERYTEXT" : "Tom", "DOCID" : "www.demo.com", "TITLE" : "Harry Potter", "TAB" : "People-Tab", "TOTALRESULTS" : "1", "DOCRANK" : 1 } { "id": .... } I am trying to execute a complex query in java. My query is to get processedClickLog collection where TAB is not equal to People-Tab

retrieving data with mongodb java driver 3.4 using find()-method with projection

社会主义新天地 提交于 2020-01-15 08:41:14
问题 I am using mongodb java driver 3.4. In the mongodb database documents are saved according to the following structure: { "_id" : ObjectId("595a9fc4fe3f36402b7edf0e"), "id" : "123", "priceInfo" : [ {object1: value1}, {object2: value2}, {object3: value3} ] } In order to retrieve the "priceInfo"-Array of a Document with a specific id, I wrote the following code: collection.find(eq("id", id)).first().projection(fields(include("priceInfo"), excludeId())); I wrote this code according too the