java mongodb 3.0:在内部数组文件中查找值

时间:2016-01-16 09:28:57

标签: java mongodb mongodb-java

我正在使用java和mongodb v.3.0.7。 我有一个带有分数的内部游戏阵列的玩家列表。这是一个插入文档的测试:

public void insertPlayer(String id_device) throws ParseException{
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
    db.getCollection("player").insertOne(
                    new Document()
                            .append("nikname", "Guest")
                            .append("password", "Guest")
                            .append("my_id", "")
                            .append("id_device", id_device)
                            .append("language", "Italian")
                            .append("games", asList(
                            new Document()
                                    .append("gamename", "PPA")
                                    .append("banned", false)
                                    .append("date", format.parse("2014-10-01T00:00:00Z"))
                                    .append("score", 11),
                            new Document()
                                    .append("gamename", "Test2game")
                                    .append("banned", false)
                                    .append("date", format.parse("2014-01-16T00:00:00Z"))
                                    .append("score", 17)))
            );
}

要查找玩家是否被禁止参加特定游戏我是否会这样做:

public boolean isBanned(String id_device){
    FindIterable<Document> iterable = db.getCollection("player").find(eq("id_device", "machine1"));

    System.out.println(iterable.first());
    List<Document> dl = (List<Document>)iterable.first().get("games");
    for(int i=0;i<dl.size();i++){
        Document d = dl.get(i);
        System.out.println(d.getString("gamename"));
        if(d.getString("gamename").equals("PPA")){
            boolean ban = d.getBoolean("banned");
            return ban;
        }
    }

使用嵌入式mongodb方法可以更快地找到文档:

new Document()
.append("gamename", "PPA")
.append("banned", false)
.append("date", format.parse("2014-10-01T00:00:00Z"))
.append("score", 11),

给出id_device和gamename? 感谢

1 个答案:

答案 0 :(得分:0)

要实现这一目标,您需要汇总数据。 https://docs.mongodb.org/manual/aggregation/

根据您的用例,聚合可能会更改(更多查询,更多管道步骤)。

以下是您的数据:

{
    "_id" : ObjectId("569a30a30586bcb40f7d2531"),
    "my_id" : "",
    "id_device" : "machine1",
    "language" : "Italian",
    "games" : [ 
        {
            "gamename" : "PPA",
            "banned" : true,
            "date" : ISODate("2014-10-01T00:00:00.000Z"),
            "score" : 11
        }, 
        {
            "gamename" : "Test2game",
            "banned" : false,
            "date" : ISODate("2014-01-16T00:00:00.000Z"),
            "score" : 17
        }
    ]
}

我假设:

You have a list of unique players. Each of them play unique games.
(Example: player1 never plays PPA twice) 
So, you need to search a game where the player is banned and return 
all information for that game.

聚合将是:

db.player.aggregate([
    {$match:{ "id_device" : "machine1"}},
    {$unwind: "$games"},
    {$match:{ "games.gamename" : "PPA", "games.banned" : true}}
])
  

结果

[ 
    {
        "_id" : ObjectId("569a30a30586bcb40f7d2531"),
        "my_id" : "",
        "id_device" : "machine1",
        "language" : "Italian",
        "games" : {
            "gamename" : "PPA",
            "banned" : true,
            "date" : ISODate("2014-10-01T00:00:00.000Z"),
            "score" : 11
        }
    }
]

有些不同,如果您的玩家可以多次玩同一个游戏(不同的日期),您可以更改聚合管道。

{
    "_id" : ObjectId("569a30a30586bcb40f7d2531"),
    "my_id" : "",
    "id_device" : "machine1",
    "language" : "Italian",
    "games" : [ 
        {
            "gamename" : "PPA",
            "banned" : false,
            "date" : ISODate("2014-10-01T00:00:00.000Z"),
            "score" : 11
        }, 
        {
            "gamename" : "Test2game",
            "banned" : false,
            "date" : ISODate("2014-01-16T00:00:00.000Z"),
            "score" : 17
        }, 
        {
            "gamename" : "PPA",
            "banned" : true,
            "date" : ISODate("2014-04-18T00:00:00.000Z"),
            "score" : 23
        }, 
        {
            "gamename" : "Foo",
            "banned" : true,
            "date" : ISODate("2015-03-03T00:00:00.000Z"),
            "score" : 2
        }, 
        {
            "gamename" : "Foo",
            "banned" : false,
            "date" : ISODate("2015-04-28T00:00:00.000Z"),
            "score" : 2
        }
    ]
}

因此,为了查询id_device和gamename“PPA”,我们以这种方式定义聚合:

db.player.aggregate([
    {$match:{ "id_device" : "machine1"}},
    {$unwind: "$games"},
    {$match:{ "games.gamename" : "PPA"}},
    {$group: {_id:{"_id":"$_id", "my_id":"$my_id", "id_device":"$id_device","language":"$language"}, "games" : {$push:"$games"}}},
    {$project: {"_id":"$_id._id", "my_id":"$_id.my_id", "id_device": "$_id.id_device", "language":"$_id.language", "games":"$games"}}
])
  

结果:

[ 
    {
        "_id" : ObjectId("569a30a30586bcb40f7d2531"),
        "games" : [ 
            {
                "gamename" : "PPA",
                "banned" : false,
                "date" : ISODate("2014-10-01T00:00:00.000Z"),
                "score" : 11
            }, 
            {
                "gamename" : "PPA",
                "banned" : true,
                "date" : ISODate("2014-04-18T00:00:00.000Z"),
                "score" : 23
            }
        ],
        "my_id" : "",
        "id_device" : "machine1",
        "language" : "Italian"
    }
]

如您所见,您可以添加/修改管道步骤并获得所需的结果。