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

java mongodb 3.0: find value in internal array of documents

我正在使用 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和游戏名? 谢谢

为此,您需要聚合数据。 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}}
])

Result

[ 
    {
        "_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 和游戏名称 "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"}}
])

Result:

[ 
    {
        "_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"
    }
]

如您所见,您可以 add/modify 管道步骤并获得所需的结果。