Mongodb查询查找带有interchangin值的数组

时间:2016-11-08 08:34:42

标签: mongodb mongodb-query

我试图找出是否可以使用带数组的列查询文档。

但是如果可能的话,这些值是互换的。是否可以搜索数组

没有正确排序值?因为我稍后会使用这个参数

更新upsert。

如果我这样做,它会起作用:

players:[ "1","2"]

但如果我这样做,则返回零:

players:["2","1"]

文件:

{
    "_id" : ObjectId("58218b1709896dabcef00cff"),
    "players" : [ 
        "1", 
        "2"
    ],
    "total_games" : 1,
    "stats" : [ 
        {
            "player_id" : "1",
            "wins" : 0
        }, 
        {
            "player_id" : "2",
            "wins" : 0
        }
    ]
}

查询(返回1):

db.getCollection('head_to_head_stats').find({players:[ "1","2"]})

查询(返回0):

db.getCollection('head_to_head_stats').find({players:[ "2","1"]})

1 个答案:

答案 0 :(得分:3)

使用$all

  

$all运算符选择a值的文档   field是一个包含所有指定元素的数组。

db.getCollection('head_to_head_stats').find({players: {$all:[ "2","1"]}})