<p>我正在用Python编写应用程序的后端,并写入PyMongo数据库。在</p>
<p>我正在设置一些服务器,我希望它们作为区块链中的节点同时运行。在我的原型中,我需要每个节点在我的数据库中为它们的区块链版本创建自己的集合,因此例如每个节点的“区块链-{insert node_id here}”。在</p>
<p>我对python相当陌生,一直在自学,但很难将“.format”方法与创建这些集合结合起来。在</p>
<p>我知道这很管用:</p>
<pre><code>client = MongoClient('mongodb://localhost:27017/')
db = client.my_blockchain
col_blockchain = db.name_of_blockchain
</code></pre>
<p>结果:名为“区块链名称”的集合</p>
<p>但当我尝试以下操作时,我得到一个错误:</p>
^{pr2}$
<p>结果:错误:</p>
<pre><code>TypeError: 'Collection' object is not callable. If you meant to call the 'format' method on a 'Collection' object it is failing because no such method exists.
</code></pre>
<p>或者当我试图将名称保存到变量中时,我无法得到动态答案:</p>
<pre><code> col_blockchain_name = 'col_my_blockchain_{}'.format(node_id)
col_blockchain = db.col_blockchain_name
</code></pre>
<p>结果:为每个运行的服务器命名为“col_blockchain_name”的集合(因此不是动态的)</p>
<p>(提前感谢,这是我的第一个堆栈溢出问题,请客气!)在</p>