Skip to content

Instantly share code, notes, and snippets.

mongoc_client_t *client = mongoc_client_new (
"mongodb://hostA,hostB/?replicaSet=my_rs");

shard01:PRIMARY> c = db.oplog.rs.find( { fromMigrate : { $exists : false } } ).addOption( DBQuery.Option.tailable ).addOption(DBQuery.Option.awaitData) { "ts" : Timestamp(1422998530, 1), "h" : NumberLong(0), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "initiating set" } } { "ts" : Timestamp(1422998574, 1), "h" : NumberLong("-6781014703318499311"), "v" : 2, "op" : "i", "ns" : "test.mycollection", "o" : { "_id" : 1, "data" : "hello" } } { "ts" : Timestamp(1422998579, 1), "h" : NumberLong("-217362260421471244"), "v" : 2, "op" : "i", "ns" : "test.mycollection", "o" : { "_id" : 3, "data" : "hello" } } { "ts" : Timestamp(1422998584, 1), "h" : NumberLong("7215322058367374253"), "v" : 2, "op" : "i", "ns" : "test.mycollection", "o" : { "_id" : 5, "data" : "hello" } } shard01:PRIMARY> c.hasNext() true shard01:PRIMARY> c.next() { "ts" : Timestamp(1423049506, 1),

>>> # Connect to one standalone, mongos, or replica set member.
>>> client = MongoClient(‘mongodb://server’)
>>>
>>> # Connect to a replica set.
>>> client = MongoClient(
... ‘mongodb://member1,member2/?replicaSet=my_rs’)
>>>
>>> # Load-balance among mongoses.
>>> client = MongoClient(‘mongodb://mongos1,mongos2’)
>>> # Two mongoses.
>>> client = MongoClient(‘mongodb://mongos1,mongos2
>>> client = MongoClient(‘mongodb://no-host.com’)
>>> client
MongoClient('no-host.com', 27017)
>>> client.db.collection.find_one()
AutoReconnect: No servers found yet
>>> try:
... MongoClient()
... print("it's working")
... except pymongo.errors.ConnectionFailure:
... print("please start mongod")
...
>>> client = MongoClient(serverSelectionTimeoutMS=500)
>>> try:
... client.admin.command('ping')
... print("it's working")
... except pymongo.errors.ConnectionFailure:
... print("please start mongod")
>>> db = client.test
>>> def thread_fn():
... db.read_preference = ReadPreference.SECONDARY