I am trying to create a webservice which receives a json object and performs some action based on whether the object already exists as a document in my mongodb or not. If the document already exists, I have to just perform my action. If it doesn't i have to insert it first and then perform the action.
Using spring data MongoRepository or MongoTemplate, how do I check if a document already exists - using the entire document I am about to insert? I cannot use _id here. If the entire document already exists, I don't have to insert a new document. But if at least one field is different, I have to enter a new document.
Eg:
@Document (collection = "foo")
class Foo {
@Id
String id;
String name;
List<Address> address
... many more attributes
}
If I get a request using an object of Foo without _id, how do I check if this already exists as a document in my mongodb collection?
Thanks!