Lists of DBObjects - Mongo Java Driver -
I am creating a game where kits in different multiplayer servers can buy player kit, where it saves in a Mongo database is. I would like to format the database:
{"server": "the_server_name", "player": {{"player": "the_players_username", "kits": ["i" I am trying to do this "," kit 1 "," kit 3 "]," player ":" the_players_username "," kits ": [" kit1 "," kit2 "]}}}
/ Pre>How to get someone to wish to start someone? Thanks!
Here's a simple example:
import com MongoDB *. Public square MongoTest {public static zero Main (string [] args throws exceptions {Mongo client mongo client = new moongo client ("localhost", 27017); db db = monoglant.gettbie ("migamedi"); dBCollection gameCollection = db.getCollection ("myGameCollection"); BasicDBObject obj = New BasicDBObject () .Append ("saver", "the_server_name"); BasicDebilist players = new basic debilist (); BasicDBList list 1 = new BasicDBList (); BasicDBList list2 = New BasicDBList (); List1.addAll (java.util.Arrays.asList (new string [] {"kit 1", "kit 2"}))); List2.addAll (java.util.Arrays.asList (new string [] {"kit 1", "kit2"})); BasicDBObject playerObj1 = New BasicDBObject ("player", "the_players_username"). Attachments ("Kit", list 1); BasicDBObject playerObj2 = New BasicDBObject ("player", "the_players_username"). Attachments ("Kit", list 2); Players.add (playerObj1); Players.add (playerObj2); Obj.append ("player", player); GameCollection.insert (obj); PrintCollectionContent (gameCollection); } Static Zero Print Control Content (DBCollection coll) {BasicDBObject Query = New BasicDBObject (); BasicDBObject field = New BasicDBObject ("server", true) .append ("_ id", incorrect) .append ("player", true); DBCursor curs = coll.find (query); While (curs.hasNext ()) {DBObject o = curs.next (); Println (o.toString ()); } Curs = coll.find (query, field); While (curs.hasNext ()) {DBObject o = curs.next (); Println (o.toString ()); }}}
BTW, your Jason
is not actually valid, the player looks like an array but in this []
Unavailable this example uses the driver version of 2.11.3
. The example shows you how to use a driver, exception handling has been omitted.
Comments
Post a Comment