0

I'm currently developing a multiplayer card game with a node.js + mongodb backend. I want users to be able to join games so I'm implementing a queue function. In this queue function I want to be able to get a single game from mongodb that is not started, not locked and doesn't contain the player in the queue.

Example of my mongodb game document:

{
"_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

}

My current query looks like this:

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

I think I should use the $not operator but I can not figure out how to use it in my use case. please someone help me!

1 Answer 1

3

Try the $ne operator since $not is only used in conjunction with another operator expression.

Sign up to request clarification or add additional context in comments.

9 Comments

I tried to do : GameBoard.findOneAndUpdate({mStarted: false, mLocked: false, mPlayers: {mPlayerId: {$ne: player.mPlayerId}}}, {mLocked: true}, function (err, gameBoard) and I tried GameBoard.findOneAndUpdate({mStarted: false, mLocked: false, mPlayers: {$ne: {mPlayerId: player.mPlayerId}}}, {mLocked: true}, function (err, gameBoard) { but none of them works like I want. The first always give me no game and the second always give me a game. How would you write it?
I think {mStarted: false, mLocked: false, mPlayers.mPlayerId: {$ne: player.mPlayerId}} should work.
It doesn't work, MongoDB/node is no big fan of "mPlayers.mPlayerId" and gives me unexpected token on the dot =/
{mStarted: false, mLocked: false, "mPlayers.mPlayerId": {$ne: player.mPlayerId}} maybe? I am just familiar with the Java driver so I have to guess (and google) what is the problem here, sorry. :)
doesn't seem to work, I get no game hits =/ I'll just have to try some more stuff. but thanks for your help!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.