繁体   English   中英

MongoDB.find() 不返回过滤结果

[英]MongoDB .find() not returning filtered result

我的以下代码忽略了过滤器。 我只想过滤国家代码“美国”,并将结果放入 dataframe。然而,当我打印 DF 的 a.head 时。 它似乎忽略了过滤器。

我的代码:

data = records.find({}, {"_id":0, "created_at":1, "text":1, "place.country_code": "US"})
filtered_data = pd.DataFrame(data)
print(filtered_data.head())

结果:

                       created_at  ...                   place
0  Fri Aug 12 10:04:00 +0000 2016  ...  {'country_code': 'US'}
1  Fri Aug 12 10:04:02 +0000 2016  ...  {'country_code': 'US'}
2  Fri Aug 12 10:04:10 +0000 2016  ...  {'country_code': 'US'}
3  Fri Aug 12 10:04:21 +0000 2016  ...  {'country_code': 'AU'}
4  Fri Aug 12 10:04:30 +0000 2016  ...  {'country_code': 'US'}

任何人都知道我做错了什么?

db.collection.find(查询,投影)

查找方法

你的“错误”:

第一个参数: query - 在您的代码中: find({}) (或find() )返回集合中的所有文档(不仅是US )。 https://docs.mongodb.com/manual/reference/method/db.collection.find/#find-all-documents-in-a-collection

第二个参数用于Projection parameter (指定要返回的字段- 而不是要返回的文档)。 https://docs.mongodb.com/manual/reference/method/db.collection.find/#projections

添加查询条件 ( country_code: "US" )。

第一个参数 - 添加查询条件

数据

[
  {
    "country_code": "US"
  },
  {
    "country_code": "US"
  },
  {
    "country_code": "AU"
  }
]

询问:

db.collection.find({
  country_code: "US"
})

结果:

[
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "country_code": "US"
  },
  {
    "_id": ObjectId("5a934e000102030405000001"),
    "country_code": "US"
  }
]

第二个参数(投影)

例如排除_id

询问:

db.collection.find({
  country_code: "US"
},
{
  _id: 0
})

结果

[
  {
    "country_code": "US"
  },
  {
    "country_code": "US"
  }
]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM