如何使用python查询mongodb上的objectid

时间:2017-03-31 15:09:08

标签: python mongodb pymongo

我正在尝试打印mongoDB中的集合中的所有对象ID。我理解如何将Private Sub Worksheet_Change(ByVal Target As Range) Dim cell As Range Set cell = Range("AK9:AL50") 'Application.EnableEvents = False Application.EnableEvents = True' If Not Application.Intersect(cell, Range(Target.Address)) Is Nothing Then If Target.Column = 37 Then ' Value in first column changed Range("AL" & Target.Row).Value = Range("AK" & Target.Row).Value / Range("V" & Target.Row).Value Exit Sub ElseIf Target.Column = 38 Then ' value in second column changed Range("AK" & Target.Row).Value = Range("AL" & Target.Row).Value * Range("V" & Target.Row).Value Exit Sub 'Application.EnableEvents = False Application.EnableEvents = True' End If End If End Sub 转换为字符串,但我不确定如何实际调用列出所有对象ID。

我在pymongo文档中尝试了以下内容,但没有任何反应,因为我有4个ObjectId s

ObjectId

2 个答案:

答案 0 :(得分:1)

from bson import ObjectId

id = "5fec2c0b348df9f22156cc07"
objInstance = ObjectId(id)
  
collection.find_one({"_id": objInstance})
  
# below line works same as the above
collection.find_one({"_id": ObjectId(id)})
collection.find_one(ObjectId(id))
安装 bson

后应该已经安装了

pymongo

答案 1 :(得分:0)

此处how you find all the docs in a collection

但你可以用python做到这一点:

all_posts = client.db.collection.find()

for post in all_posts:
    print post._id