package awesomefb;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;

import java.net.UnknownHostException;

/**
 * Created by earl on 5/25/2015.
 */
public class DatabaseManager {
    private static DatabaseManager instance = null;
    private DB database;
    private DBCollection postsCollection;
    private DBCollection usersCollection;

    public static DatabaseManager getInstance() {
        if (instance == null) {
            try {
                MongoClient mongoClient = new MongoClient();
                DB db = mongoClient.getDB("awesomefb");

                instance = new DatabaseManager(db);
            } catch (UnknownHostException e) {
                System.out.println(e.toString());
            }
        }
        return instance;
    }

    protected DatabaseManager(DB database) {
        this.database = database;

        postsCollection = this.database.getCollection("posts");
        usersCollection = this.database.getCollection("users");

        postsCollection.drop();
        usersCollection.drop();
    }

    public void insertPost(Post post) {
        postsCollection.insert(post.toDBObject());
    }

    public void insertUser(User user) {
        usersCollection.insert(user.toDBObject());
    }
}

@@ -36,75 +36,9 @@
 odb.
-DB;%0Aimport com.mongodb.DBCollection;%0Aimport com.mongodb.MongoClient
+*
 ;%0A%0Ai
@@ -1162,31 +1162,121 @@
     
-usersCollection.insert(
+String facebookId = user.getFacebookId();%0A        usersCollection.update(new BasicDBObject(%22fb_id%22, facebookId), 
 user
@@ -1280,27 +1280,40 @@
 ser.toDBObject()
+, true, false
 );%0A    %7D%0A%7D%0A

Insert user only if not exists