spring-data mongodb exclude fields from update - mongodb

How to make sure that specific fields can be inserted upon creation, but can optionally be excluded when updating the object.
I'm essentially looking for something like the following:
mongoOperations.save(theObject, <fields to ignore>)
From what I see, recently introduced #ReadOnlyProperty will ignore the property both for inserts and updates.
I was able to get the desired behavior by implementing my Custom MongoTemplate and overriding its doUpdate method as follows:
#Override
protected WriteResult doUpdate(String collectionName, Query query,
Update originalUpdate, Class<?> entityClass, boolean upsert, boolean multi) {
Update updateViaSet = new Update();
DBObject dbObject = originalUpdate.getUpdateObject();
Update filteredUpdate = Update.fromDBObject(dbObject, "<fields to ignore>");
for(String key : filteredUpdate.getUpdateObject().keySet()){
Object val = filteredUpdate.getUpdateObject().get(key);
System.out.println(key + "::" + val);
updateViaSet.set(key, filteredUpdate.getUpdateObject().get(key));
}
return super
.doUpdate(collectionName, query, updateViaSet, entityClass, upsert, multi);
}
But the issue is that now it will use Mongo $set form of updates for everything, not just for specific cases.
Please advise if there is any simpler (and correct) way to achieve this.

While creating an update object, use $setOnInsert instead of $set. It is available in spring mongo as well.
If an update operation with upsert: true results in an insert of a document, then $setOnInsert assigns the specified values to the fields in the document. If the update operation does not result in an insert, $setOnInsert does nothing.

Related

update multiple documents in mongodb from spring mongo

In my use case I want to update multiple documents at once, documents that match a query, using spring-data-mongo.
Here is what I have been trying,
Criteria filterCriteria = new Criteria().andOperator(Criteria.where("bac").is("def"));
Update update = new Update();
update.set("status", status);
Query query = new Query();
query.addCriteria(filterCriteria);
mongoOperations.findAndModify(query, update, MyClass.class);
But this is not updating any document.
Plus I have looked up in the mongo documentation but have not anything useful
https://docs.mongodb.com/manual/reference/method/db.collection.findAndModify/#comparisons-with-the-update-method
Here is the version that I am using
Mongodb - 3.6
spring-data-mongodb - 1.5.5.RELEASE
findAndModify(...) method can update a document and return either the old or newly updated document in a single operation.
To update all document that matches the given query use updateMulti(...).
https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/MongoOperations.html#updateMulti-org.springframework.data.mongodb.core.query.Query-org.springframework.data.mongodb.core.query.UpdateDefinition-java.lang.Class-
visit the link and there you will find it.
#Autowire
MongoTemplate mongoTemplate;
Query query = new Query();
Criteria filterCriteria = Criteria.where("bac").is("def");
query.addCriteria(filterCriteria);
Update update = new Update();
update.set("status", status);
mongoTemplate.updateMulti(query, update, MyClass.class);

Mongodb $set and $inc in camel

I am attempting to use the $set and $inc flags to update a field and increment a field by 1 in camel ( java ). Here is the code I am using:
from("direct:mySampleRoute").routeId("update-mongo-db-entry").setBody()
.simple("{\"person\": \"${headers.personName}\", \"job\": \"computerGuy\",\"title\": \"DR\"},"
+ "{\"$set\": {\"pay\": \"${headers.newPay}\"}, \"$inc\": {\"hrPay\": 1}}")
.toD("mongodb:mongoClient?database=" + mongoDb + "&collection="
+ mongoEmplyeeCollection + "&operation=update")
.end();
The goal of the query is to find an entry where person == ${headers.personName}, job == computerGuy, and title = DR. Once it is found, it is to set the pay field to ${headers.newPay} and inc the hrPay field by positive 1. From what I can tell, I am structuring my code exactly the same as mentioned in this post: What am I doing wrong with $set and $inc in update However, once executed, my app crashes and I am unable to see any logs wrt to why that query fails. As such, I suspect I have a fundamental problem in structuring the json update. Camel documentation located here is of limited help.
Using camel, how can I achieve my goal of making a "select" query, updating some fields using $set, and incrementing a field using $inc ?
I ended up solving my problem using a Processor to create the needed body. I slved it using this syntext within the process method:
#Override
public void process(final Exchange exchange) throws Exception {
final BasicDBObject filterQuery = new BasicDBObject();
filterQuery.append("person", exchange.getIn().getHeader("personName"));
filterQuery.append("job", "computerGuy");
filterQuery.append("title", "DR");
final BasicDBObject updateQuery = new BasicDBObject();
updateQuery.append("$set", new BasicDBObject("pay", exchange.getIn().getHeader("newPay"))
.append("location", "4th floor"));
updateQuery.append("$inc", new BasicDBObject("hrPay", 1));
final List<DBObject> query = new ArrayList<>();
query.add(filterQuery);
query.add(updateQuery);
exchange.getIn().setBody(query);
}

Update Value in Mongo 3.6.0 while updating value inside json object

I want to update FieldA in someFields while someFields is null in MongoDB
{
"someFileds : {
"FieldA" : "ABS"
}
}
someFields may have more fields as well but Just want to ensure that other fields won't be overridden.
Sometime SomeFields won't be having any fields so want to
I am using below code for set the the value however value for someFields is null in MongoDB.
BasicDBObject dbObject = new BasicDBObject();
dbObject.put("SomeFields.FieldA", "TEST");
Could you please suggest how can handle the null in this scenario.
Use this Mongo update query.
db.collection.update({}, {$set:{"someFields": {"FieldA": "Test"}}});
It will create FieldA in someFields when it is null otherwise will only update FieldA.
In your case kindly pass object all time in 2nd parameter of put.
dbObject.put("SomeFields", {FieldA:"TEST"});

MongoDB Morphia Update multi=true VS bulk update

I want to update 10,000 documents in a single web request. I intend to update only one field (which is indexed) in all the documents matched with some criteria with same value.
I see morphia 1.3.2 always set multi=true parameter in update call. Is it enough for updating 10,000 document? Or there are any bulk update functionality in morphia.
The following code should work for you.
Query<Entity> query = datastore.createQuery(Entity.class);
query.filter("name = ", "xxx");
UpdateOperations<Entity> updateOperations = datastore.createUpdateOperations(Entity.class).set
("yyy", 200);
UpdateResults updateResults = datastore.update(query, updateOperations, false, null);
All the documents in the collection with name = 'xxx' will now have all their 'yyy' attribute equal to 200.

update a field with the value of other field

How can I update a field with the value of other field?
For example, I want to update the field updated_at with the value of the field created_at (it's like cloning fields)
$dm->createQueryBuilder('MyBundle:MyService')
->update()
->multiple(true)
->field('updated_at')->set(CREATED_AT_ATRIBUTE)
->getQuery()
->execute();
Thanks
Option #1: Application side update
The easiest approach when using Doctrine ODM is to perform the update on the application side. So you fetch all the objects you want to update, make the necessary adjustments and flush them.
$services = $dm->createQueryBuilder('MyBundle:MyService')
->getQuery()
->execute();
foreach ($services as $service) {
$service->setUpdatedAt($service->getCreatedAt());
}
$db->flush();
$services represents the mongo cursor, which will fetch the documents as you iterate. You can set eagerCursor to true if you want to fetch all the documents from the collection at once.
Option #2: Database side update
You can also perform the update directly on the database itself. To do so however, you'll need to create the query yourself as the query builder doesn't support this functionality.
// Get MongoDB instance from DocumentManager
$mongo = $dm->getDocumentDatabase('Fully/Qualified/Class/Name')
->getMongoDB();
// Iterate over the collection and update each document
$mongo->execute('
db.MyServiceCollection.find().forEach(function (item) {
db.MyServiceCollection.update(
{_id: item._id},
{$set: {updated_at: item.created_at}}
);
});
');
The function used in forEach is just a regular javascript function, so you can insert some more logic in there if you need more control over what you update.