javalistmongodbdriverdbobject

Lists of DBObjects - Mongo Java Driver


I am making a game where there are kits in different multiplayer servers. Players can purchase kits, where it saves it to a Mongo database. I would like the database to be formatted like so:

{
    "server": "the_server_name",
    "players": {
        {
            "player": "the_players_username",
            "kits": [
                "kit1",
                "kit3"
            ]
        },
        {
            "player": "the_players_username",
            "kits": [
                "kit1",
                "kit2"
            ]
        }
    }
}

I have tried doing this with no avail. Would anybody like to offer a beginner some help on how to achieve this? Thanks!


Solution

  • Here is a simple example:

    import com.mongodb.*;
    
    public class MongoTest {
    
        public static void main(String[] args) throws Exception {
            MongoClient mongoClient = new MongoClient("localhost", 27017);
            DB db = mongoClient.getDB("myGameDB");
            DBCollection gameCollection = db.getCollection("myGameCollection");
            BasicDBObject obj = new BasicDBObject().append("sever", "the_server_name");
            BasicDBList players = new BasicDBList();
            BasicDBList list1 = new BasicDBList();
            BasicDBList list2 = new BasicDBList();
            list1.addAll(java.util.Arrays.asList(new String[]{"kit1", "kit2"}));
            list2.addAll(java.util.Arrays.asList(new String[]{"kit1", "kit2"}));
            BasicDBObject playerObj1 = new BasicDBObject("player", "the_players_username").append("kits", list1);
            BasicDBObject playerObj2 = new BasicDBObject("player", "the_players_username").append("kits", list2);
            players.add(playerObj1);
            players.add(playerObj2);
            obj.append("players", players);
            gameCollection.insert(obj);
            printCollectionContent(gameCollection);
        }
    
    
        static void printCollectionContent(DBCollection coll) {
            BasicDBObject query = new BasicDBObject();
            BasicDBObject fields = new BasicDBObject("server", true).append("_id", false).append("players",true);
            DBCursor curs = coll.find(query);
            while (curs.hasNext()) {
                DBObject o = curs.next();
                System.out.println(o.toString());
            }
            curs = coll.find(query, fields);
            while (curs.hasNext()) {
                DBObject o = curs.next();
                System.out.println(o.toString());
            }
        }
    }
    

    BTW, your json is actually not valid. Players seems to be an array but it is missing a [ ]

    This example uses the driver version of 2.11.3. The example is to show you how to use the driver, the exception handling is omitted.