Mongodb:查找具有至少一个元素与 ObjectID 不匹配的数组的文档 [英] Mongodb: Find documents with array where at least one element does not match ObjectID
问题描述
我正在为 MongoDB 请求而苦苦挣扎.我有一个 Play
架构,其中包含一组 Move
对象.每个 Move
对象都以 ObjectID 的形式保存对 Player
的引用.关注这个问题 我试着做
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 } } }
其中 playerId
保存 ObjectID.但是我得到了错误
where playerId
holds an ObjectID. However I get the error
Error: Can't use $elemMatch with ObjectId
我也尝试了以下
{ 'moves.player.str': { $elemMatch : { $ne : playerId.toString() } } }
但它没有找到合适的文件...有什么想法吗?
but it doesn't find the proper documents... Any ideas?
示例
一些播放
记录:
A = {
"moves": [
{ player: { $oid: "56f32fe2f41638de3b3e4773" } },
{ player: { $oid: "56f32fe2f41638de3b3e4774" } }
]
}
B = {
"moves": [
{ player: { $oid: "56f32fe2f41638de3b3e4773" } }
]
}
查询playerId = "56f32fe2f41638de3b3e4773"
应该只返回对象A,因为它是唯一一个拥有至少一个玩家不同于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
.
推荐答案
好的,找到问题所在了...就去做
OK, found out where the problem was... Just do
{ "moves": { $elemMatch: { "player": { $ne : playerId } } } }
效果很好!
这篇关于Mongodb:查找具有至少一个元素与 ObjectID 不匹配的数组的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!