I am struggling with a MongoDB request. I have a Play schema that holds an array of Move objects. Each Move object holds a reference to a Player in the form of an ObjectID. Following this question I tried to do
{ 'moves.player': { $elemMatch : { $ne : playerId } } }
where playerId holds an ObjectID. However I get the error
Error: Can't use $elemMatch with ObjectId
I have also tried the following
{ 'moves.player.str': { $elemMatch : { $ne : playerId.toString() } } }
but it doesn't find the proper documents... Any ideas?
Example
Some Play records:
A = {
"moves": [
{ player: { $oid: "56f32fe2f41638de3b3e4773" } },
{ player: { $oid: "56f32fe2f41638de3b3e4774" } }
]
}
B = {
"moves": [
{ player: { $oid: "56f32fe2f41638de3b3e4773" } }
]
}
Query for playerId = "56f32fe2f41638de3b3e4773" should only return object A, since it is the only one that has an array of moves where at least one of the players is different from 56f32fe2f41638de3b3e4773.