MongoDB查找用户不在数组中

时间:2013-03-15 22:04:42

标签: node.js mongodb

我目前正在开发一个带有node.js + mongodb后端的多人纸牌游戏。我希望用户能够加入游戏,所以我正在实现队列功能。在这个队列函数中,我希望能够从mongodb获得一个未启动,未锁定且不包含队列中的玩家的游戏。

我的mongodb游戏文档示例:

{
"_id": {
    "$oid": "512cccf9e4b09000a6f1f079"
},
"mChanceTaken": false,
"mCurrentPlayer": 0,
"mCurrentPlayerName": "-",
"mDeck": [
    {
        "mValue": 13,
        "mSuit": "HEARTS"
    },
    {
        "mValue": 13,
        "mSuit": "SPADES"
    },
    {
        "mValue": 3,
        "mSuit": "SPADES"
    },
    {
        "mValue": 10,
        "mSuit": "SPADES"
    },
    {
        "mValue": 11,
        "mSuit": "CLUBS"
    },
    {
        "mValue": 3,
        "mSuit": "HEARTS"
    },
    {
        "mValue": 7,
        "mSuit": "DIAMONDS"
    },
    {
        "mValue": 9,
        "mSuit": "SPADES"
    },
    {
        "mValue": 8,
        "mSuit": "HEARTS"
    }
],
"mFinished": false,
"mLocked": false,
"mNumberOfPlayers": 4,
"mPlayers": [
    {
        "mPlayerId": "512bd9a1e4b09000a6f1f073",
        "mUsername": "user2",
        "mPosition": 0,
        "mSwitching": true,
        "mFaceUp": [],
        "mFaceDown": [],
        "mHand": []
    },
    {
        "mPlayerId": "512bcb3be4b09000a6f1f06b",
        "mUsername": "user1",
        "mPosition": 0,
        "mSwitching": true,
        "mFaceUp": [],
        "mFaceDown": [],
        "mHand": []
    }
],
"mRoundLength": 60,
"mStarted": false,
"mSwitching": false

}

我当前的查询如下所示:

GameBoard.findOneAndUpdate({mStarted: false, mLocked: false, mPlayers: {$not: {mPlayerId: player.mPlayerId}}}, {mLocked: true}, function (err, gameBoard) {

我想我应该使用$ not运算符,但我无法弄清楚如何在我的用例中使用它。请有人帮助我!

1 个答案:

答案 0 :(得分:3)

尝试使用$ne运算符,因为$not仅与其他运算符表达式一起使用。