Get affected rows after Mongo update - java

How can I instruct Mongo to return the affected rows after doing an update?
Currently it returns
{ "serverUsed" : "localhost:27017" , "ok" : 1 , "n" : 12 , "updatedExisting" : true}
which means 12 records was updated, but I want to find out which records they were.
One workaround is to do two queries, first to find the _id's, and second to actually update them but this is not good enough.
Many thanks
R.

Use the findAndModify() API. For example, following the guideline from this blog post MongoDB findAndModify() examples using Mongo Shell and Java Driver, you can write your update query as:
// findAndModify operation. Update colour to blue for cars having speed < 45
DBObject query = new BasicDBObject("speed",
new BasicDBObject("$lt", 45));
DBObject update = new BasicDBObject();
update.put("$set", new BasicDBObject("color", "Blue"));
DBCursor cursor = coll.find();
try {
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
coll.findAndModify(query, update);

Related

Multi field updates in document in java mongodb

Below is my mongob document structure
{
"employee_id":"123",
"employee_name":"ABC",
"elements":[
{
"element_name":"Verification Id",
"element_value":"Test Address",
"element_status":"selected"
},
{
"element_name":"Reportees",
"element_value":["ABC","DEF"],
"element_status":"selected"
}
,
{
"element_name":"Countries",
"element_value":["China","USA"],
"element_status":"selected"
}
]
}
My input will be {"countries":["Russia","Japan"],"Verification Id":"license"}.
My requirement is to update the only the countries and Verification Id element values for the given employee id. Reportees should remain untouched.
What are the ways to achieve this using java mongodb.
I tried following approaches
1)
collection.updateMany(Filters.and(Filters.eq("employee_id", "123"),Filters.eq("elements.element_name", "Verification Id"))
,
Updates.combine(
Updates.set("elements.$.element_value", "license"),
Updates.set("elements.$.element_status", "changed"),
));
and
collection.updateMany(Filters.and(Filters.eq("employee_id", "123"),Filters.eq("elements.element_name", "Countries")))
,
Updates.combine(
Updates.set("elements.$.element_value", ["Russia","Japan"]),
Updates.set("elements.$.element_status", "changed"),
));
The disadvantage here is that I end up making 2 db calls. If more such parameters come as input, then the number of db calls to perform the update increases
2.)
List<DBObject> array = new ArrayList<DBObject>();
BasicDBObject bObj=new BasicDBObject();
bObj.put("element_name", "Countries");
bObj.put("element_value", ["Russia","Japan"]);
bObj.put("element_status", "changed");
BasicDBObject bObj1=new BasicDBObject();
bObj1.put("element_name", "Verification Id");
bObj1.put("element_value", "license");
bObj1.put("element_status", "changed");
array.add(bObj);
array.add(bObj1);
collection.updateMany(Filters.eq("employee_id", "123"),
Updates.combine(
Updates.set("elements", array)
));
The disadvantage is that it completly over writes the elements array and removes the array element "element_name":"Reportees".
PLease guide me further on this. PLease let me know the best way to update select fields based on different criteria.

How to delete mongodb document with two conditions one with $gt operator?

I would like to retrieve the following information:
delete from database where name = 'AAA' and age>20;
but for MongoDB in Java. Essentially, it should delete the document that contain the word AAA and age greater than 20 in them. I know that there is the $in operator in MongoDB, but how do I do the same in Java, using the Java driver? I've been trying to look for it everywhere but am getting nothing. I've tried:
query = new BasicDBObject("age", new BasicDBObject("$gt", "20"), new BasicDBObject("name", "AAA"));
JSON which i want to delete is like this.
{"school" : "NewSchool" , "name" : "AAA" , "age" : "50"}
What you want is the find-term:
{
"name" : "AAA",
"age" : { $gt : 20 }
}
Construct this as your basic DB object, or simply use the new 3.x Filters to create the Bson for you. (As I personally only use 3.x, here's the appropriate example):
MongoClient client = ...
MongoDatabase db = client.getDatabase(...);
MongoCollection<Document> coll = db.getCollection(...);
coll.deleteMany(Filters.and(Filters.eq("name", "AAA"), Filters.gt("age", 20)));

MongoDB querying with Java API

Here is the sample document of my MongoDB:
user:{
_id:1,
name:'xyz',
age:12,
mobile:21321312,
transaction:[{
trans_id:1,
prod:'a',
purchasedAt:ISODate("2015-02-01"),
},
{
trans_id:2,
prod:'b',
purchasedAt:ISODate("2015-02-01")
},
{
trans_id:3,
prod:'c',
purchasedAt:ISODate("2014-11-24")
}]
,...
}
My query looks like:
db.user.find({transaction:{$elemMatch:{prod:'a', purchasedAt:ISODate("2015-02-01")}}, transaction:{$elemMatch:{prod:{$nin:['b','c']}, purchasedAt:ISODate("2015-02-01")}}}).count()
I am trying to get the user count who have purchased product 'a' on date "2015-02-01" but not have purchased product b & c on same day.
So while trying to do this in Java with the query:
coll.find(new BasicDBObject().append("transaction", new BasicDBObject("$elemMatch", new BasicDBObject("prod", 'a').append("purchasedAt", Date))).append("transaction", new BasicDBObject("$elemMatch", new BasicDBObject("prod", new BasicDBObject("$nin",['b','c'])).append("purchasedAt", Date)));
I have also tried:
coll.find(new BasicDBObject("transaction", new BasicDBObject("$elemMatch", new BasicDBObject("prod", 'a').append("purchasedAt", Date))).append("transaction", new BasicDBObject("$elemMatch", new BasicDBObject("prod", new BasicDBObject("$nin",['b','c'])).append("purchasedAt", Date)));
where Date is "2015-02-01" in util.Date object.
I found out that Java ignores the $in part of the query, i.e. it ignores {transaction:{$elemMatch:{prod:'a', purchasedAt:ISODate("2015-02-01")}} & performs only $nin part.
I found out it by DBCursor object.
Here's the output of the cursor:
Cursor: Cursor id=0, ns=mydb.user, query={ "transaction" : { "$elemMatch" : { "prod" : { "$nin" : [ "b" , "c"]} , "purchasedAt" : { "$date" : "2015-02-01T00:00:00.000Z"}}}}, numIterated=0, readPreference=primary
Because of this my result is inaccurate. I wonder why the exact same query works well in Mongo shell but doesn't with Java API. Is there anything wrong with my query structure?
My guess is that this question is now moot, but, if you still do not consider it answered, are you looking for the "$not" operator, which can check for non-existance sort of.

Add new data to existing collection in mongo in java

I need two to add new items to the existing data in mongo db.
This is mongo db I have the following data.
{
"_id" : ObjectId("53ce11e7d0881d32d9fa935f"),
"name" : "massive riots",
"lastFeachedTime" : "Jul 15, 2014 12:55:27 PM"
}
Here I have to find the data based on name and the I have to add another two items two it.
Here is my code.
DBObject queryObject = new BasicDBObject().append("name", keyword);
if (null == newFetchTime) {
}
DBObject updateObject = new BasicDBObject();
updateObject.put("nextPageToken", nextPageToken);
updateObject.put("prevPageToken", prevPageToken);
Utils utils = new Utils();
DBCollection collection = utils.getStaging().getCollection("test");
collection.update(queryObject, updateObject, true, false);
But I am do update the existing value get removed and the new data get added.
Can any one tell me how to add the items to the existing data in mongo db.
You want the $set operator in your update. This allows the specified fields to be altered without affecting any of the existing fields in the document, unless the specified field exists in which case that field is overwritten:
DBObject update = new BasicDBObject(
"$set", new BasicDBObject()
.append("nextPageToken",nextPageToken)
.append("prevPageToken",prevPageToken)
);
Works out to the equivalent in shell:
{ "$set" : { "nextPageToken" : nextPageToken , "prevPageToken" : prevPageToken }}

get some attributes from mongodb except one or two

I would like to get some information which is in a mongoDB except some attributes.
I tried it in cmd and it worked:
db.orders.find({name:"chabeee"},{_id:0, name:1, worksAt:1})
Then I get this result:
{ "name" : "chabeee", "worksAt" : "jobAtBp" }
{ "name" : "chabeee", "worksAt" : "jobAtRE" }
Its okay, but I want to get in a Java Program. How can I do that?
You have to create one additional BasicDBObject, which will be used for pointing out which exact keys to be fetched. And finally the DBCollection#find(DBObject ref, DBObject keys) method has to be invoked in order to pass the desired projection keys.
BasicDBObject query = new BasicDBObject("name", "chabeee");
BasicDBObject keys = new BasicDBObject();
keys.put("_id", 0);
keys.put("name", 1);
keys.put("worksAt", 1);
BasicDBCursor result = collection.find(query, keys);
Then you just have to iterate over the BasicDBCursor and verify the result.
while (cursor.hasNext()) {
System.out.println(cursor.next());
}

Categories