joe
joe

Reputation: 53

Spring boot MongoRepository saveAll() only if doesn't exist

I'm trying to use save multiple documents in Spring boot data mongoDB with saveAll() but not sure if there is a way to verify if the documents exist before inserting to prevent duplicate entries. If I use save() instead, I should be able to avoid duplicate entries by checking if documents exist prior to each insertion. But instead of call save() multiple times I want to use saveAll() but I'm unable to check if any of the documents exist before inserting. Does any one think this would be possible?

for the initial insert I can use saveAll() an insert the below document all at once

[
    {
        "model": "Highlander",
        "make": "Toyota",
        "year": "2022",
        "color": "Black",
        "condition": "Used"
    },
    {
        "model": "Explorer",
        "make": "Ford",
        "year": "2020",
        "color": "Silver",
        "condition": "Used"
    },
    {
        "model": "Acadia",
        "make": "GMC",
        "year": "2023",
        "color": "Black",
        "condition": "New"
    }
]

and if I try to re-insert this same document with saveAll(), it would go through without a problem but it would be a duplicate document and I want to prevent that and what I'm trying to figure out is would it be possible to check if the document exist before saveAll() takes place to prevent duplicate entry.

Upvotes: 0

Views: 206

Answers (0)

Related Questions