查询mongoose - 查找opponet id等于或challanger id等于some_id的所有战斗

时间:2017-07-29 11:57:21

标签: javascript node.js mongoose

我试图查询我的mongo db,了解玩家玩过的所有战斗。我想知道猫鼬的操作或操作。我无法让它发挥作用。

我尝试过这样的事情:

var currentUser = req.params.player_id;

Battle.find({}).populate({
             path: 'utfordrer motstander',
             }).where('utfordrer.id').equals(currentUser).$or('motstander.id').equals(currentUser).exec(function (err, battle) {
                if (err){
                    console.log(err);
                }
                else {
                    console.log(battle);
                }
        });

这是我的战斗模式:

   var battleSchema = new mongoose.Schema({
   utfordrer: {
      id: {type: mongoose.Schema.Types.ObjectId, ref: "User"},
      username: String,
      score: Number,
      ferdig: Boolean
   },
   motstander: {
      id: {type: mongoose.Schema.Types.ObjectId, ref: "User"},
      username: String,
      score: Number,
      ferdig: Boolean
   },
   spill:
       {
           id: {type: mongoose.Schema.Types.ObjectId, ref: "Game"},
           navn: String,
           beskrivelse: String
       },
   tidspunkt: Date
});

1 个答案:

答案 0 :(得分:0)

Battle.find({$or:[ {'utfordrer.id': currentUser}, {'motstander.id': currentUser}]}).exec(function (err, battle) {
            if (err){
                console.log(err);
            }else{
                console.log("===========UTFORDRINGER");
                console.log(battle.length);
               res.render("Battles/index", {antallUtfordringer: antall, battleId: utfordringBattleId});
            }
});