Mongodb:在我的python代码中分享一个集合

时间:2021-10-03 03:00:19

I have a distributed MongoDB 3.2 database. The system is mounted on two nodes with RedHat operating system.

我有一个分布式的MongoDB 3.2数据库。系统安装在具有RedHat操作系统的两个节点上。

Using python and the PyMongo driver (or some other), I want enable the sharding of a collection, specifying the compound shard key.

使用python和PyMongo驱动程序(或其他一些),我想启用集合的分片,指定复合分片键。

In the mongo shell this works:

在mongo shell中,这可以工作:

> use mongotest
> sh.enableSharding("mongotest")
> db.signals.createIndex({ valueX: 1, valueY: 1 }, { unique: true })
> sh.shardCollection("mongotest.signals", { valueX: 1, valueY: 1 })

('mongotest' is the DB, and 'signals' is the collection)

('mongotest'是数据库,'信号'是集合)

The last two lines I want to make them within my code. Does anyone know if this is possible in python? If so, how is it done?

我想在我的代码中创建它们的最后两行。有谁知道这在python中是否可行?如果是这样,它是如何完成的?

thank you very much, sorry for my bad English

非常感谢,抱歉我的英语不好

1 个解决方案

#1


0  

A direct translation of your shell commands to python is as shown below.

将shell命令直接转换为python如下所示。

from pymongo import MongoClient


client = MongoClient() 
db = client.admin # run commands against admin database.

db.command('enableSharding',  'mongotest')
db.command({'shardCollection': 'mongotest.signals', 'key': {'valueX': 1, 'valueY': 1}})

However, you may want to confirm that both enableSharding and shardCollection are exposed in your db by running

但是,您可能需要确认通过运行在db中公开enableSharding和shardCollection

db.command('listCommands')

#1


0  

A direct translation of your shell commands to python is as shown below.

将shell命令直接转换为python如下所示。

from pymongo import MongoClient


client = MongoClient() 
db = client.admin # run commands against admin database.

db.command('enableSharding',  'mongotest')
db.command({'shardCollection': 'mongotest.signals', 'key': {'valueX': 1, 'valueY': 1}})

However, you may want to confirm that both enableSharding and shardCollection are exposed in your db by running

但是,您可能需要确认通过运行在db中公开enableSharding和shardCollection

db.command('listCommands')