简体   繁体   English

DBObjects列表-Mongo Java驱动程序

[英]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. 玩家可以购买套件,并将其保存到Mongo数据库中。 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! 谢谢!

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. 顺便说一句,您的json实际上无效。 Players seems to be an array but it is missing a [ ] 玩家似乎是一个数组,但缺少[ ]

This example uses the driver version of 2.11.3 . 本示例使用2.11.3的驱动程序版本。 The example is to show you how to use the driver, the exception handling is omitted. 该示例向您展示如何使用驱动程序,省略了异常处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM