如何在MongoDB中调用文档

时间:2015-04-11 22:34:26

标签: python mongodb

我很难理解mongodb的概念。为什么我无法通过其ID加载文档?

    # Start Client and mongostuff
    client = MongoClient()
    db = client.NAMEOFDATABASE
    a_collection = gridfs.GridFS(db, collection="a_collection")
    b_collection = db.b_collection

    # This works !!!
    a_collection.find_one({"_id": ObjectId("552990a75ce9fd691c2fe412")})

    # Why not this ?????????
    id = b_collection.find({"Content": "A"}).distinct("_id_IMAGE")
    a_collection.find_one({"_id": ObjectId(id)})

为什么我这样做:我想在mongodb中保存文本和一些图像。所以我保存图像,然后使用img_ids保存文本。然后我计划调用文本和文本中的ID我存储的图像。

    outputdata = a_collection.get(_id_IMAGE).read()
    # create an output file and store the image in the output file
    outfilename = "image_from_mongodb.jpg" 
    output= open(outfilename,"w")
    output.write(outputdata)

感谢您的帮助!

0 个答案:

没有答案