Below is the code I have:
//Creating a mongo client and connecting to the database and getting the collection from the table in database
MongoClient mongoClient = new MongoClient("000.1.1.26", 27017);
MongoDatabase databases = mongoClient.getDatabase("DBName");
System.out.println("Connected to the database successfully");
MongoCollection<Document> collection = databases.getCollection("TableName");
// Retrieving Column-wise data for fieldName from database
BasicDBObject searchQuery = new BasicDBObject();
FindIterable<Document> testColumn=collection.find().projection(Projections.fields(Projections.include("fieldName"), Projections.excludeId()));
//We have to run this command(Here I have merged 2 commands )
AggregateIterable<Document> iterDoc = collection.aggregate(Arrays.asList(Aggregates.group("$Id", Accumulators.max("logDate", 1)), project(Projections.fields(Projections.include("fieldName"), Projections.excludeId()))));
The above joint query is giving me below error:
The method project(Bson) is undefined for the type
Any help for this?
Related
Trying to fetch data from mongodb using java but my code will take more time to fetch the record.
the mongodb data is very big 1 million record there in mongo db.
MongoDatabase database = config.getCollection();
String docName = slpitDName[i];
MongoCollection<Document> collection = database.getCollection(docName);
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put("flag", 0);
List<Document> documents = (List<Document>) collection.find(whereQuery).into(new
ArrayList<Document>());
I'm new to mongoDB and trying to use mongo database for my desktop application which I use javaFX as frontend.The version of mongo-java-driver is 3.10.1 and up to now can add data to db successfully.I want to perform Roll Back if any of textfields are empty.isValidText boolean check whether textfields are empty.Do I really need to roll back?If so how can I perform roll back?
MongoClient mongoClient = new MongoClient("localhost", 27017);
DB db = mongoClient.getDB("testDB");
DBCollection coll = db.getCollection("test");
BasicDBObject doc = new BasicDBObject("name", nameTxt.getText().toString())
.append("type", typeTxt..getText().toString())
.append("count", countText.getText().toString())
.append("info", infoTxt.getText().toString());
if (isValidText){
coll.insert(doc);
}else{
System.out.println("Text Fields are empty");
}
I am trying to build a simple lookup of a mongodb database using the below code. On runtime, the code runs but never returns a value.
The generated query works in the command line no problem. The database is quite large, around 2.8gb.
public static void main(String[]args){
String customer="peter";
String job="builder";
// To connect to mongodb server
MongoClient mongoClient=new MongoClient("localhost",27017);
// Now connect to your databases
MongoDatabase db=mongoClient.getDatabase("customers");
System.out.println("Connect to database successfully");
// Now connect to your collection
MongoCollection<Document> collection=db.getCollection("customerData");
System.out.println("Connect to collection");
BasicDBObject andQuery=new BasicDBObject();
List<BasicDBObject> obj=new ArrayList<BasicDBObject>();
obj.add(new BasicDBObject("job",job));
obj.add(new BasicDBObject("customer",customer));
andQuery.put("$and",obj);
System.out.println(andQuery.toString());
FindIterable<Document> iterable=collection.find(new Document(andQuery));
MongoCursor<Document> iterableDocument=iterable.iterator();
while(iterableDocument.hasNext()){
Document wholeDocument=(Document)iterableDocument.next();
System.out.println("1: "+wholeDocument.get("age"));
System.out.println("2: "+wholeDocument.get("dob"));
}
}
Try simplifying your query as
DBObject obj = new BasicDBObject();
obj.put( "job", job );
obj.put( "customer", customer );
collection.find(new Document(obj))
Also, getDatabase and getCollection don't do a connect. If you misspelled those parts, the query will return nothing because you are hitting an empty database. (the first connect attempt is when you call 'find'. If the database or collection don't exist, Mongo will create it if you write to it)
This mongodb query below returns the most recent entries to a collection
db.RSS.find().limit(6).sort({$natural:-1}).pretty()
Does anyone know how to implement this query in Java?
Using Mongo-Java Driver, this code is an example:
MongoClient client = new MongoClient("localhost",27017);
MongoDatabase db = client.getDatabase("test");
MongoCollection<Document> collection = db.getCollection("RSS");
FindIterable<Document> it = collection.find().limit(6).sort(new Document().append("$natural", -1));
MongoCursor<Document> cursor = it.iterator();
while(cursor.hasNext()){
Document doc = cursor.next();
System.out.println(doc.toJson());
}
I am wondering if MongoDB provide some good schema for identifying documents in the database. Suppose I have initialized a database like this:
public static void main(String[] args) {
MongoClient mongo = new MongoClient("localhost", 27017);
DB db = mongo.getDB("testdb");
DBCollection coll = db.getCollection("mycollection");
BasicDBObject document = new BasicDBObject();
document.put("name", "Mike");
document.put("age", 25);
coll.insert(document);
}
Now How can I get the "document" inserted in the database? I already know a key/value pair query can locate the document, like:
BasicDBObject query = new BasicDBObject();
query.put("name", "Mike");
DBObject dbObj = coll.findOne(query);
Is there another way other than this kind of key/value pair query to identify the document?
If you want to identify a collection, you can do it by match the collection name:
DBCollection coll = db.getCollection("mycollection");
I hope there is something like this to identify the document. Any suggestion?
If you look in the document object after the coll.insert(document); you will find that a _id field was added by the driver. You can use this field/value to query for the exact document since MongoDB enforces there must be an _id field and it must be unique.
Putting this all together:
public static void main(String[] args) {
MongoClient mongo = new MongoClient("localhost", 27017);
DB db = mongo.getDB("testdb");
DBCollection coll = db.getCollection("mycollection");
BasicDBObject document = new BasicDBObject();
document.put("name", "Mike");
document.put("age", 25);
coll.insert(document);
System.out.println(document.get("_id"));
BasicDBObject query = new BasicDBObject();
query.put("_id" , document.get("_id"));
DBObject retrieved = collection.findOne(query);
System.out.println(retrieved);
}