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);
Related
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?
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 have a small query which is giving the result while invoking it on mongo database. But when I am using this in JAVA for fetching the data then it giving me exception.
Below is the query :
db.collectionName.find({'name': 'Sam'},{"Address": { "$slice": -1 } })
In database, name is the key and address is the list containing lets say 4 number of addresses.We want to fetch the updated address in "Address" KEY.
Below is the java code which we are using :
final DBCollection dbCollection = mongoTemplate.getCollection("apMonitoringData");
final BasicDBObject query = new BasicDBObject();
query.put("name", "sam");
query.put("address", new BasicDBObject("$slice", -1));
final BasicDBObject sortQuery = new BasicDBObject();
// Sorting in Descending order for last updated entry
sortQuery.put("_id", -1);
final DBCursor dbCursor = dbCollection.find(query).sort(sortQuery).limit(1);
DBObject dbObject = null;
while (dbCursor.hasNext()) {
dbObject = dbCursor.next();
}
return dbObject;
but it is giving error as
com.mongodb.MongoException: Can't canonicalize query: BadValue: unknown operator: $slice
Can anybody please look into this.
You will need to separate the query from the fields.
Also, use append when you want to add entries to a document instead of put
BasicDBObject query = new BasicDBObject("name","sam");
BasicDBObject fields = new BasicDBObject("address",new BasicDBObject("$slice", -1));
collection.find(query,fields).sort(sortQuery).limit(1);
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);
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?