// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package nosql.mongodb;

import org.bson.Document;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class Test3 {
    private static void dump(MongoCollection<Document> col, Document criteria) {
        System.out.println("dumping " + criteria.toJson());
        for(Document d : col.find(criteria)) {
            System.out.println(d.toJson());
            /*
            System.out.printf("Id       : %d\n", d.getInteger("no"));
            System.out.printf("Customer : %s\n", d.getString("customer"));
            for(Document d2 : d.getList("items", Document.class)) {
                System.out.printf("  %2d %-30s %10s %4d %s\n", d2.getInteger("no"),
                                                               d2.getString("name"),
                                                               d2.getString("price"),
                                                               d2.getInteger("quantity", 1),
                                                               d2.getString("note") != null ? d2.getString("note") : "");
            }
            */
        }
    }
    public static void main(String[] args) {
        // open
        MongoClient client = new MongoClient("localhost", 27017);
        MongoDatabase db = client.getDatabase("TestDB");
        MongoCollection<Document> col = db.getCollection("java_json");
        // setup
        String s1 = "{ \"id\": 1, \"customer\": \"A A\", \"items\": [ { \"no\": 1, \"name\": \"A good Java book\", \"price\": \"19.95\" } ] }";
        System.out.println(s1);
        Document d1 = Document.parse(s1);
        col.insertOne(d1);
        String s2 = "{ \"id\": 2, \"customer\": \"B B\", \"items\": [ { \"no\": 1, \"name\": \"ASUS MB\", \"price\": \"249.00\" }, " +
                                                                    "{ \"no\": 1, \"name\": \"i5 CPU\", \"price\": \"299.00\", \"note\": \"handle with care\" }, " +
                                                                    "{ \"no\": 1, \"name\": \"4 GB kit\", \"price\": \"249.00\", \"quantity\": 4} ] }";
        System.out.println(s2);
        Document d2 = Document.parse(s2);
        col.insertOne(d2);
        // query
        dump(col, Document.parse("{ }")); // all
        dump(col, Document.parse("{ \"id\": 2 }")); // where id=2
        dump(col, Document.parse("{ \"customer\": \"A A\" }")); // where customer='A A'
        dump(col, Document.parse("{ \"customer\": { \"$regex\": \"A.*\"} }")); // where customer like 'A%'
        dump(col, Document.parse("{ \"items.name\": \"i5 CPU\" }")); // where items.name='i5 CPU'
        dump(col, Document.parse("{ \"items.name\": { \"$regex\": \".*Java.*\" } }")); // where items.name like '%Java%'
        dump(col, Document.parse("{ \"id\": { \"$in\": [ 1, 2 ] } }")); // where id in (1,2)
        dump(col, Document.parse("{ \"items.note\": { \"$exists\": true } }")); // where exists items.note
        // close
        client.close();
    }
}
