2017-04-21 166 views
0

如何使用pythonMongoDB中搜索特定字符串

集合帖过滤字符串682186760在DB:

{ 
    "_id" : ObjectId("58f9efa9c9948f002449b8f7"), 
    "a" : [ 
     "682186760" 
    ] 
} 

为如

if "682186760" in posts.find(): 
    print("yes") 
+3

[MongoDB:如何找出数组字段是否包含元素?](http://stackoverflow.com/questions/16198429/mongodb-how-to-find-out-if-an-array -field-contained-an-element) – JJJ

+1

只需使用查询posts.find({a:682186760}),对于我来说你在做'if“682186760”posts.find(): 打印(“是”)' – AshokGK

回答

0

这里是蒙戈查询为:

db.collection.find(
    { a: { $elemMatch: {'$eq':'682186760'} } } 
) 

PyMongo查询将为:

db.collection.find(
     { 'a': { '$elemMatch': {'$eq':'682186760'} } } 
    ) 

您将获得游标,您可以在其中找到与查询匹配的所有元素。

+0

感谢这对我工作! –