Mongodb 如何在基于数组值的集合中查找文档
我尝试添加一个帮助器以返回包含文档的数组,如下所示:Mongodb 如何在基于数组值的集合中查找文档,mongodb,meteor,Mongodb,Meteor,我尝试添加一个帮助器以返回包含文档的数组,如下所示: documents = { realPlayers: [], subscribers: [ {playerId: }, {playerId: }, {playerId: } ], privatGame:, gameType:, gameStatus: 'active' } 我试过这个,但不起作
documents = {
realPlayers: [],
subscribers: [
{playerId: },
{playerId: },
{playerId: }
],
privatGame:,
gameType:,
gameStatus: 'active'
}
我试过这个,但不起作用(forEach也一样):
Template.myGames.helpers({
“myGames”:函数(){
设gamePul=[],
activeGames=Games.find({gameStatus:“active”});
for(var i=0;i
您只是想找到当前用户订阅的游戏集,对吗?您只需在查询中使用$elemMatch
:
Template.myGames.helpers({
myGames: function() {
return Games.find({ gameStatus: "active",
subscribers: { $elemMatch: { playerId: Meteor.userId() }}});
}
});
你从
activeGames
中得到了什么回报?应该是一个光标,您可以在上使用.forEach
。你有什么结果吗?是的,你完全正确,它完全起作用了!谢谢
Template.myGames.helpers({
myGames: function() {
return Games.find({ gameStatus: "active",
subscribers: { $elemMatch: { playerId: Meteor.userId() }}});
}
});