Mongdb skipping inserts when creating initial collection - java

Seems that when the collection is initially created there a period where you can't insert docs. So running the code below I only get the first insert.
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB( "workqueue" );
DBCollection coll = db.getCollection("jobs");
coll.insert(new BasicDBObject("desc", "test1"));
coll.insert(new BasicDBObject("desc", "test2"));
coll.insert(new BasicDBObject("desc", "test3"));
However, if I put a Thread sleep in between each insert I do get all 3. Is this a bug or am I doing something wrong?

Related

Combine two mongoDB queries in Java

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?

records saved in mongodb disappeared after some minutes

Updated records got missing after awhile in mongodb
I tried to update a document in mongodb using java driver 3.6, st first the records got updated successfully. but after 1 minutes those records value turned to an empty string
A = new A();
BasicDBObject searchQuery = new BasicDBObject("username", username);
BasicDBObject updateFields = new BasicDBObject();
updateFields.append("fullnames", agent.getFullnames());
BasicDBObject setQuery = new BasicDBObject();
setQuery.append("$set", updateFields);
getLiveagentCollection().update(searchQuery, setQuery);
The updated records shoould remain persisted on the DB
I was able to resolve this issue, by adding writeconcern within the mongodb.
MongoClient client = new MongoClient("localhost", 27112);
client.setWriteConcern(WriteConcern.MAJORITY);

Roll back data before saving to mongoDB database java standalone application

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");
}

Mongodb Java query never returns a value

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)

How to concurrently read and update a field of mongodb from java

Let's take an example of facebook posts where multiple users give likes to a post. Now for some post currently database stores 10 likes and if two users liked that post concurrently then both will read 10 as current like and update 11 one by one but the correct value should be 12. How to achieve this using mongodb and java.
Check the operator $inc.
Here is a sample code from this link:
// connect to MongoDB server.
Mongo mongo = new Mongo("localhost", 27017);
DB database = mongo.getDB("mydb");
DBCollection collection = database.getCollection("testCollection");
// create a simple db object where counter value is 0
DBObject temp = new BasicDBObject("name", "someName").append("counter", 0);
// insert it into the collection
collection.insert(temp);
// create an increment query
DBObject modifier = new BasicDBObject("counter", 1);
DBObject incQuery = new BasicDBObject("$inc", modifier);
// create a search query
DBObject searchQuery = new BasicDBObject("name", "someName");
// increment a counter value atomically
WriteResult upRes = collection.update(searchQuery, incQuery);

Categories

Resources