pymongo:如何使用$或运算符作为数组的列?

Bin*_*hen 5 python mongodb pymongo

我有这样的集合:

user_id     albums
1           [1 2 3 4]
2           [3 5 7 8]
Run Code Online (Sandbox Code Playgroud)

我想查找所有记录,相册包含3或7或8,我写了这样的代码,但没有工作:

or_array = []
or_array.append({"albums":3})
or_array.append({"albums":7})
or_array.append({"albums":8})

collection1.find({"$or":or_array})
Run Code Online (Sandbox Code Playgroud)

这样做的正确方法是什么?

dsk*_*ner 7

试试这个

collection1.find({'albums': {'$in': [3, 7, 8]}})
Run Code Online (Sandbox Code Playgroud)

来自mongodb文档, [IN] allow[s] you to specify an array of possible matches

如果这不起作用,可能会回溯并查看集合中的实际类型3 78确保它们是整数.

print type(collection1.find_one()['albums'][0])
Run Code Online (Sandbox Code Playgroud)