Reputation: 6500
I'm trying to realize check existing documents in mongoDB, which I need to update then (it's not duplicates just older documents) using Java driver 3.4.
My snippet of code is:
// search feed
Document found = database.getCollection("rss_feed").find(new Document("title", title)
.append("url", url)
.append("img", img)
.append("price", price)).first();
if (found == null) {
collection.insertOne(new Document("title", title)
.append("url", url)
.append("img", img)
.append("price", price));
mongoClient.close();
System.out.println("Feed not exists in database. Written.");
} else {
System.out.println("Feed exists in database.");
mongoClient.close();
}
But in this way, I'm adding just new documents without updating. (As I understand, it's good, when cluster will check itself and delete without me older documents during overflow "document size")
When I'm trying to update older documents via adding check in else condition:
// search feed
Document found = database.getCollection("rss_feed").find(new Document("title", title)
.append("url", url)
.append("img", img)
.append("price", price)).first();
if (found == null) {
collection.insertOne(new Document("title", title)
.append("url", url)
.append("img", img)
.append("price", price));
mongoClient.close();
System.out.println("Feed not exists in database. Written.");
} else {
collection.updateOne(eq(found), new Document("title", title)
.append("url", url)
.append("img", img)
.append("price", price));
mongoClient.close();
System.out.println("Feed exists in database. Updated");
mongoClient.close();
}
I'm getting:
WARNING: Unsupported option 'retrywrites' in the connection string
Update:
If I write else if
condition with count
method to check number of documents:
else if(collection.count() > 36) {
collection.updateOne(found, new Document("updated title", title)
.append("updated url", url)
.append("updated img", img)
.append("updated price", price));
System.out.println("Feed completely updated in database.");
then else if
will be skipped:
Feed already exists in database.
Feed already exists in database.
Feed already exists in database.
Feed already exists in database.
Feed already exists in database.
Does anyone know how to write check documents to update them, if they're existing ?
Upvotes: 0
Views: 215
Reputation: 6500
There's a few ways how to realize it:
Capped collections automatically remove the oldest documents in the collection without requiring scripts or explicit remove operations.
To replace a document, pass a new document to the replaceOne method.
Using updateOne/updateMany methods. In my case, I needed just to update several fields, so I wrote like:
collection.updateOne(eq("_id", user_id), new Document("$set", new Document("View links", "On")));
In some cases where you may need to update many fields in a document, it may be more efficient to replace the document.
Upvotes: 0
Reputation: 106
The retryWrites
connection parameter was added in MongoDb 3.6 according to this. As you said yourself, you are using version 3.4, hence the warning.
Somewhere in your code you probably have a connection string that looks similar to this: mongodb://db1.example.net:27017?retryWrites=true
. You either need to upgrade to a version >= 3.6 or remove the retryWrites
parameter from the connection string in order to get rid of the warning.
Upvotes: 1