简体   繁体   English

如何操作文档中的特定字段(PyMongo)

[英]How to manipulate a specific field in a document (PyMongo)

For example, I have a field in my document called "x", which contains a giant string. 例如,我在文档中有一个名为“ x”的字段,其中包含一个巨大的字符串。 I want to be able to read the string word by word. 我希望能够逐字读取字符串。

client = pymongo.MongoClient("localhost", 27017)
db = client.clients
collection = db.unlabel_review

for post in collection.find():
    documents.append(post)

So this returns the entire JSON object list, and calling a certain index on documents will return that document. 因此,这将返回整个JSON对象列表,并且在文档上调用某个索引将返回该文档。 What is the syntax to return a field of that document, for example return "id" or field "x" where x holds a string? 返回该文档字段的语法是什么,例如,返回“ id”或字段“ x”(其中x包含字符串)?

Intuitively, I want to say 直观地说,我想说

documents[1] -> returns whole JSON object so

documents[1][1] -> returns the first index of that JSON object, but this doesn't seem to work

Thanks for any help :) 谢谢你的帮助 :)

I figured it out, 我想到了,

documents[1]["field name"] will return a specific field. document [1] [“字段名称”]将返回特定字段。

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

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