I'm new in MongoDb and I have to upload a file in MongoDB with a java program.
I tried to write something but I do not know if it's the right way.
Can someone help me?
My difficulty is that the json file I have to upload is in a link address.
can mongodb read and upload the document to a database through java language?
I load the code below.
Thank you.
import java.io.IOException;
import java.util.Iterator;
import org.bson.Document;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.util.JSON;
public class mongodb {
public static void main (String args []) throws IOException {
MongoClient mongo = null;
MongoDatabase db = null;
try {
/**** Connect to MongoDB ****/
mongo = new MongoClient("localhost", 27017);
/**** Get database ****/
db = mongo.getDatabase("db_plant");
System.out.println("Successfully connected to database");
} catch (Exception e) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
DBCollection collection = db.getCollection("plants");
final String URL = "https://www.plants.usda.gov/java/downloadData?fileName=plantlst.txt&static=true";
String json = getTextFromUrl(URL);
DBObject dbObject = (DBObject)JSON.parse(json);
collection.insert(dbObject);
DBCursor cursorDocJSON = collection.find();
while (cursorDocJSON).hasNext() {
System.out.println(cursorDocJSON).next();
}
collection.remove(new BasicDBObject();
}
private static String getTextFromUrl(String uRL) {
// TODO Auto-generated method stub
return null;
}
}
You are using the old API.
If you use the Mongo java driver 3.X, the correct api is :
MongoCollection<Document> collection = database.getCollection("plants");
Document dbObject = Document.parse(json);
MongoCursor<Document> cursor = collection.find().iterator();
Related
I have mongodb 4.0 install in my Ubuntu 18.04.
I am using java-mongo-driver 3.12.1
I am uploading my code you can see that i have check different type of connections but it's not working.
How to solve this problem?
Give this a try, let me know what you think...
package test;
public class Main {
public static void main(String[] args) {
com.mongodb.client.MongoClient client = connectToStandAlone();
com.mongodb.client.MongoDatabase db = client.getDatabase("javatest");
QueryData(db);
}
private static com.mongodb.client.MongoClient connectToStandAlone() {
// STANDALONE STILL REQUIRES HOSTS LIST WITH ONE ELEMENT...
java.util.ArrayList<com.mongodb.ServerAddress> hosts = new java.util.ArrayList<com.mongodb.ServerAddress>();
hosts.add(new com.mongodb.ServerAddress("127.0.0.1", 27017));
com.mongodb.MongoCredential mongoCredential = com.mongodb.MongoCredential.createScramSha1Credential("testuser", "admin", "mysecret".toCharArray());
com.mongodb.MongoClientSettings mongoClientSettings = com.mongodb.MongoClientSettings.builder()
.applyToClusterSettings(clusterSettingsBuilder -> clusterSettingsBuilder.hosts(hosts))
.credential(mongoCredential)
.writeConcern(com.mongodb.WriteConcern.W1)
.readConcern(com.mongodb.ReadConcern.MAJORITY)
.readPreference(com.mongodb.ReadPreference.nearest())
.retryWrites(true)
.build();
com.mongodb.client.MongoClient client = com.mongodb.client.MongoClients.create(mongoClientSettings);
return client;
}
private static void QueryData(com.mongodb.client.MongoDatabase db) {
// DRIVER DOES NOT HAVE collection.findOne().
com.mongodb.client.MongoCollection<org.bson.Document> collection = db.getCollection("people");
com.mongodb.client.MongoCursor<org.bson.Document> cursor = collection.find(com.mongodb.client.model.Filters.eq("testfield", true))
.sort(new org.bson.Document("review_date", -1))
.skip(5)
.limit(20)
.iterator();
while(cursor.hasNext()) {
org.bson.Document document = cursor.next();
String json = document.toJson();
System.out.println(json);
boolean testfieldValue = document.getBoolean("testfield");
String ssnValue = document.getString("ssn");
}
}
}
I found simple solution.
I have added more jar files,in total we need (bson-3.12.1.jar, mongodb-driver-core-3.12.1.jar, mongodb-driver-sync-3.12.1.jar, mongodb-driver-sync-3.12.1-javadoc.jar). These are available in enter link description here
Now my code is working fine
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import java.util.Arrays;
public class MongoConnection {
public static void main(String[] args) {
try {
// Create Connection
MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");
System.out.println("MongoDB connected successfully");
//Accessing the database
MongoDatabase database = mongoClient.getDatabase("java");
System.out.println("MongoDB database connection ");
//Creating a collection
database.createCollection("first");
System.out.println("Collection created successfully");
//Accessing collection
MongoCollection<Document> collection = database.getCollection("first");
Document document = new Document("name", "Café Con Leche")
.append("contact", new Document("phone", "228-555-0149")
.append("email", "cafeconleche#example.com")
.append("location",Arrays.asList(-73.92502, 40.8279556)))
.append("stars", 3)
.append("categories", Arrays.asList("Bakery", "Coffee", "Pastries"));
collection.insertOne(document);
} catch (Exception e) {
e.printStackTrace();
}
}
}
I am trying to create a simple connection and insert a document to local MongoDB instance using java (eclipse 4.7.1a). MongoDB is up & running and able to do operations via "Compass" as well.
package com;
import com.mongodb.MongoClient;
import com.mongodb.WriteConcern;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;
import java.util.Arrays;
public class connectToDB {
public static void main( String args[] ) {
try{
// To connect to mongodb server
MongoClient mongoClient = new MongoClient( "localhost" , 27017
);
// Now connect to your databases
DB db = mongoClient.getDB("test");
System.out.println("Connect to database successfully");
// if collection doesn't exists, MongoDB will create it for you
DBCollection coll = db.getCollection("testCollection");
System.out.println("Collection coll selected successfully");
// insert
List<Integer> books = Arrays.asList(27464, 747854);
DBObject documents = new BasicDBObject("_id", "jo")
.append("name", "Jo Bloggs")
.append("address", new BasicDBObject("street", "123 Fake St")
.append("city", "Faketon")
.append("state", "MA")
.append("zip", 12345))
.append("books", books);
coll.insert(documents);
System.out.println("Document added to Collection coll successfully");
// update
BasicDBObject query = new BasicDBObject();
query.put("name", "Xo");
BasicDBObject newDocument = new BasicDBObject();
newDocument.put("name", "Jo Bloggs-updated");
BasicDBObject updateObj = new BasicDBObject();
updateObj.put("$set", newDocument);
coll.update(query, updateObj);
System.out.println("Document updated to Collection coll successfully");
// find - search
BasicDBObject searchQuery1 = new BasicDBObject();
searchQuery1.put("name", "mkyong");
DBCursor cursor = coll.find(searchQuery1);
System.out.println("Selection from Collection coll done successfully");
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
// delete
BasicDBObject searchQuery2 = new BasicDBObject();
searchQuery2.put("name", "Xo");
coll.remove(searchQuery2);
System.out.println("Document deleted from Collection mycol2 successfully");
}catch(Exception e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
}
But when executed, ends with following error in eclipse.
Nov 01, 2017 2:19:31 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE,
requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms',
maxWaitQueueSize=500}
Exception in thread "main" java.lang.NoSuchMethodError:
org.bson.BsonDocument.clone()Lorg/bson/BsonDocument;
at com.mongodb.connection.ClientMetadataHelper.
createClientMetadataDocument(ClientMetadataHelper.java:159)
at com.mongodb.connection.ClientMetadataHelper.
createClientMetadataDocument(ClientMetadataHelper.java:149)
at com.mongodb.connection.InternalStreamConnectionFactory.<init>
(InternalStreamConnectionFactory.java:37)
at com.mongodb.connection.DefaultClusterableServerFactory.
create(DefaultClusterableServerFactory.java:58)
at com.mongodb.connection.BaseCluster.createServer(BaseCluster.java:359)
at com.mongodb.connection.SingleServerCluster.<init>
(SingleServerCluster.java:52)
at com.mongodb.connection.DefaultClusterFactory.
createCluster(DefaultClusterFactory.java:147)
at com.mongodb.Mongo.createCluster(Mongo.java:726)
at com.mongodb.Mongo.createCluster(Mongo.java:720)
at com.mongodb.Mongo.<init>(Mongo.java:290)
at com.mongodb.Mongo.<init>(Mongo.java:285)
at com.mongodb.Mongo.<init>(Mongo.java:281)
at com.mongodb.MongoClient.<init>(MongoClient.java:186)
at com.mongodb.MongoClient.<init>(MongoClient.java:163)
at com.mongodb.MongoClient.<init>(MongoClient.java:153)
at com.connectToDB.main(connectToDB.java:25)
MongoDB version : v3.4.9
Following jar files used. bson 3.0.4.jar & mongo-java-driver-3.5.0.jar
Could anyone help me out with on why this is been giving this error?
Your codes are working perfectly before you use your MongoDB code run simple hello world program,
public class connectToDB {
public static void main( String args[] ) {
System.out.println("Hello world");
}
}
if it works then put your MongoDB codes in the main and run it again. in case if it does not work then the first thing you need to learn "how to use Eclipse IDE?" before you jump into the database.
Here is the result of your codes.
here is how your data looks like in the database.
I deleted 'bson-3.0.2.jar' from referenced libraries of the code, and it works.
As the error message said,
NoSuchMethodError:
org.bson.BsonDocument.clone()Lorg/bson/BsonDocument;
it seems that certain method dependency has been crashed between bson jar and mongodb java driver.
Spec: mongo-java-driver-3.3.0.jar,jdk1.7,Mongodb 3.0.12
MongoShell : db.getCollection("Table-WEBSRVS-DTLS").find({"col1":"1000","col4":"EMEA"},{"col1":"1","col2":"1"})
Question : How to achieve this mongoshell command in Java for Mongo-java 3.x API ?
thx
Here is the equivalent Java code for the above query. You may need to change the database and collection names accordingly in the below code.
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Projections;
public class GetDataFromTableWebsrvsDtls {
public static void main(String[] args) {
MongoClient client = new MongoClient();
MongoDatabase database = client.getDatabase("localhost");
MongoCollection<Document> collection = database.getCollection("TableWebsrvsDtls");
FindIterable<Document> collectionData = collection
.find(Filters.and(Filters.eq("col1", "1000"), Filters.eq("col4", "EMEA")))
.projection(Projections.include("col1", "col2"));
for (Document doc : collectionData) {
System.out.println(doc.toJson());
}
client.close();
}
}
I have a document like this in a database in Mongo DB.
{
"_id" : ObjectId("5800d904a3e7535f0d2d673a"),
"username" : "sai",
"password" : "sai123"
}
{
"_id" : ObjectId("5800d921a3e7535f0d2d673b"),
"username" : "surya",
"password" : "surya123"
}
Now I have a html which takes in username and password.
How do I save the username and password individually and store them into separate strings from the database in MongoDB?
When I'm trying to query the data using "username SAI" as the key, from DB using a Java code like this:
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.ServerAddress;
import java.util.Arrays;
public class MongoDBJDBC {
public static void main(String args[]){
try{
MongoClient client=new MongoClient("localhost", 27017);
DB db= client.getDB("test");
System.out.println("Connect to database successfully");
DBCollection coll= db.getCollection("login");
System.out.println("Collection POST selected successfully");
String uname="username";
String s="sai";
DBCursor cursor= coll.find(new BasicDBObject(uname, s),new BasicDBObject("_id", 0));
int i=1;
while(cursor.hasNext()){
System.out.println("Inserted doc :" +i);
DBObject xyz= cursor.next();
System.out.println(xyz);
i++;
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
I get this OUTPUT:
Connect to database successfully
Collection POST selected successfully
Inserted doc :1
{ "username" : "sai" , "password" : "sai123"}
How can I manipulate these operations to store username and password into temporary local strings?
Please advise me.
UPDATE:
I've put the entire code. Please help me. I'm new too this.
To implement such mechanism you will need to:
Create an index composed of your 2 fields with coll.ensureIndex(new BasicDBObject("username", 1).append("password", 1)), it allows to get the best possible performances when it will execute your query.
Get the first document that matches with the provided username and password, if a document can be found the username/password are ok otherwise they are incorrect.
The code could be:
BasicDBObject query = new BasicDBObject("username", uname).append("password", s);
// Gets only the id of a doc that matches with the username and password
DBObject item = coll.findOne(query, new BasicDBObject("_id", Boolean.TRUE));
// If != null ok, ko otherwise
if (item == null) {
// KO
} else {
// OK
}
The needed imports:
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCollection;
NB: Storing directly passwords like this in a db is not a good approach for security reason they should be slated and hashed
I am trying to insert multiple records to MongoDB at once which is return by web service in JSON format.
But I got the following error :
INFO: Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 0, 4]}, minWireVersion=0, maxWireVersion=3, electionId=null, maxDocumentSize=16777216, roundTripTimeNanos=545196}
Exception in thread "main" java.lang.IllegalArgumentException: BasicBSONList can only work with numeric keys, not: [_id]
at org.bson.types.BasicBSONList._getInt(BasicBSONList.java:168)
at org.bson.types.BasicBSONList._getInt(BasicBSONList.java:160)
at org.bson.types.BasicBSONList.get(BasicBSONList.java:105)
at com.mongodb.DBCollection.insert(DBCollection.java:309)
at com.mongodb.DBCollection.insert(DBCollection.java:284)
at com.mongodb.DBCollection.insert(DBCollection.java:250)
at com.mongodb.DBCollection.insert(DBCollection.java:187)
at santosh.GoPharmaTest.main(GoPharmaTest.java:80)
I have used the following code , Please suggest what I do to get the result ?
package santosh;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.util.JSON;
public class GoPharmaTest {
#SuppressWarnings("deprecation")
public static void main(String[] args) {
String response="";
MongoClient mongoclient=new MongoClient("localhost", 27017);
DB db = mongoclient.getDB("admin");
DBCollection collection = db.getCollection("product");
String url = "user web service";
URL obj;
try {
obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
//con.setRequestProperty("User-Agent", USER_AGENT);
//int responseCode = con.getResponseCode();
//StringBuffer response = new StringBuffer();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response+=inputLine;
}
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBObject dbObject = (DBObject)JSON.parse(response);
collection.insert(dbObject);
DBCursor cursorDoc = collection.find();
while (cursorDoc.hasNext()) {
System.out.println(cursorDoc.next());
}
System.out.println("Done");
}
}
BasicDBList can't be used to do inserts of multiple documents, it's only used for arrays inside a single document. To do a bulk insert, you need to pass an array of DBObjects into the insert method instead.
as per java doc the insert() can accept either single DBObject or an array or List of them.
So, in order to save, you need to convert your JSON array into an array/List of DBObjects, or save each array's item