猫鼬-穿过物体 [英] Mongoose - go through object

查看:96
本文介绍了猫鼬-穿过物体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在node.js上使用猫鼬,我试图找到所有玩家game.players.id等于我传递的ID的游戏.

Using mongoose on node.js I'm trying to find all games where player game.players.id equals the id I passed.

架构:

var Game = mongoose.Schema({

    id: String,
    date: { type: Date, default: Date.now },
    game: Object,
    isOnline: Boolean

});

我不确定此函数有什么问题,但它返回空数组:

I'm not sure what is wrong in this function but it returns empty array:

var specificGameStatistics = function (user, game) {
    var deferred = q.defer()
    Game.find({ "game.players.id" : user, "game.rules.gameType": game.gameType, "game.rules.quatro": game.quatro}, function(err, data) {
        deferred.resolve(data);
    });
    return deferred.promise;
}
////////////////////USAGE///////////////
var testGame = {rules: {gameType : 1, quatro : null}}
UsersCtrl.specificGameStatistics(data.id, testGame).then(function(userData) {
    console.log(userData);
});

这是已经保存在数据库中的游戏示例:

And here is the example of the game already saved in database:

{
"isOnline" : true,
"game" : {
    "numberOfPlayers" : NumberInt("1"),
    "players" : [
        {
            "id" : "58a2c0ecd8ba9f8602836870",
            "name" : "PlayerName",
            "type" : NumberInt("1"),
            "avgStatistic" : "30.00",
            "numbersHit" : NumberInt("1"),
            "totalScore" : NumberInt("60"),
            ..............................
        }
    ], //there is more players here
    "rules" : {
        "gameType" : NumberInt("1"),
        "quatro" : null,
        "rounds" : NumberInt("1"),
    } // there is more in JSON object
    ...............................
"_id" : ObjectId("58aed4aeea20ecdf0c426838"),
"date" : ISODate("2017-02-23T13:25:18.284+01:00"),
"__v" : NumberInt("0")
}

我已经测试了玩家ID是否相等,但是仍然返回空数组. 测试代码:

I have tested the player ID to be equal and it is but still it returns empty array. Test code:

///////////TEST//////////////
console.log(data.id, "58a2c0ecd8ba9f8602836870");
if (data.id === "58a2c0ecd8ba9f8602836870") {console.log("this is true");}
var testGame = {rules: {gameType : 1, quatro : null}}
UsersCtrl.specificGameStatistics(data.id, testGame).then(function(userData) {
    console.log(userData);
});
//////////TEST///////////////

,它返回:
58a2c0ecd8ba9f8602836870 58a2c0ecd8ba9f8602836870
这是真的
[]

and it returns:
58a2c0ecd8ba9f8602836870 58a2c0ecd8ba9f8602836870
this is true
[]

答案:在DeividasKaržinauskas的帮助下,解决方案是:

Answer: With help of Deividas Karžinauskas the solution is:

Game.where('game.players.id', user).where('game.rules.gameType', game.rules.gameType).find({}, function(err, data) { //, "game.rules.quatro": game.quatro
    deferred.resolve(data);
});

推荐答案

这是因为您指定的其他规则({gameType : 1, quatro : null})在播放器对象(

This is because of the additional rules that you specify ({gameType : 1, quatro : null}), which do not exist in the player object (

{
    "id" : "58a2c0ecd8ba9f8602836870",
    "name" : "PlayerName",
    "type" : NumberInt("1"),
    "avgStatistic" : "30.00",
    "numbersHit" : NumberInt("1"),
    "totalScore" : NumberInt("60"),
    ..............................
}

).您可以通过按ID查找游戏来确认这一点.

). You can confirm this by simply looking for a game by id.

如果要添加这些规则,则应找到所有符合这些规则的游戏,然后寻找特定玩家的游戏.

If you want to add these rules then you should find all games which match these rules and then look for the games of a specific player.

这篇关于猫鼬-穿过物体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆