我正在使用 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"
}
]
如您所见,您可以添加/修改管道步骤并获得所需的结果。
关于java mongodb 3.0 : find value in internal array of documents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34825450/