invzbl3
invzbl3

Reputation: 6450

How to avoid copying documents using mongodb Java driver

I need to check info that will be added to database using Java driver 3.4+ and avoid copies. In my case, documents will be like:

{
  title: "Household Portable Steam Brush Mini Iron Handheld Ironing Machine"
  url: "https://..."
  img: "..."
  price: "..."
}

Supposed that each document will be unique. So, I tried to wrote using FindIterable simply like:

long found = collection.count(Document.parse(title));
FindIterable<Document> iterable = database.getCollection("rss_feed").find(new Document("title", title)
            .append("url", url)
            .append("img", img)
            .append("price", price));
// check if feed not exists
if (found == 0) {
    Document doc = new Document("title", title)
            .append("url", url)
            .append("img", img)
            .append("price", price);
    collection.insertOne(doc);
    mongoClient.close();

    System.out.println("Feed not exists in database. Written.");
    // to check existing feed and add new feed
} else if (iterable.equals(found)) { // error
    Document doc = new Document("title", title)
            .append("url", url)
            .append("img", img)
            .append("price", price);
    collection.insertOne(doc);
    mongoClient.close();
    // if feed exists
} else {
    System.out.println("Feed exists in database.");
    mongoClient.close();
}

In this case I'm getting an error: enter image description here

Also tried to create hash-index and check via it:

 long found = collection.count(Document.parse(title));
 String indexField  = database.getCollection("rss_feed").createIndex(Indexes.hashed("_id"));

        // check if feed not exists
        if (found == 0) {
            Document doc = new Document("title", title)
                    .append("url", url)
                    .append("img", img)
                    .append("price", price);
            collection.insertOne(doc);
            mongoClient.close();

            System.out.println("Feed not exists in database. Written.");
            // added new feed
        } else if (indexField.equals(found)) { // error
            Document doc = new Document("title", title)
                    .append("url", url)
                    .append("img", img)
                    .append("price", price);
            collection.insertOne(doc);
            mongoClient.close();
            // if feed exists
        } else {
            System.out.println("Feed exists in database.");
            mongoClient.close();
        }

Also getting:

enter image description here

Does anyone know how to realize check if documents with fields is unique in Java? Or what is better to use? I appreciate any help.

Upvotes: 1

Views: 94

Answers (1)

invzbl3
invzbl3

Reputation: 6450

As mentioned by @Veeram, I can realize check unique documents using

insert when the document with field combination is not found.

 Document found = database.getCollection("rss_feed").find(fields).first();
    if (found == null) { 
    collection.insertOne(fields); 
    mongoClient.close(); 
    System.out.println("Feed not exists in database. Written."); 
    } else { 
    System.out.println("Feed exists in database.");
     mongoClient.close(); 
    }

Upvotes: 1

Related Questions