pymongo

Add a validator to a Mongodb collection with pymongo

泪湿孤枕 提交于 2019-12-09 18:32:16
问题 I am trying to add a validator to a MongoDB collection using pymongo. The command I would like to run adapted from here Is equivalent to this: db.runCommand( { collMod: "contacts", validator: { phone: { $type: 'string' } }, validationLevel: "moderate" } ) { "ok" : 1 } And subsequently will throw an error if a non-string datatype is inserted tin the phone field Using python I did the following: db.command({'collMod': 'contacts', 'validator': {'phone': {'$type': 'string'}}, 'validationLevel':

importerror: no module named flask.ext.script

谁说胖子不能爱 提交于 2019-12-09 16:57:30
问题 In fact I cannot use any pakage now! importerror: no module named flask.ext.script importerror: no module named Pymongo 回答1: It seems that you virtual environment doesn't work. You've installed the flask-script package, but when you run the script, it still looks for it in C:\Python3.4 . You may give us more info so that we can figure it out where is wrong. (How do you install it, how do you active the virtualenv, does reinstall virtualenv work, close the cmd shell and try again works?) Also

convert pymongo cursor to json

北慕城南 提交于 2019-12-09 14:02:19
问题 I know this is a fairly common problem. I'm writing a small Flask app and I'm trying to feed some queries back to the view. I've connected to my local MongoDB setup, and made a successful query - but I can't generate a json object with it. The most common solution I've seen is to import json_util from pymongo i.e. import json from pymongo import json_util results = connection.get_collection('papayas_papaya') results = results.find({ 'identifier': '1', }) serialized_results = [json.dumps

Redis 实现排行榜

邮差的信 提交于 2019-12-09 13:19:47
不再介绍数据库做实时排行榜的弊端,直接介绍Redis的有序集合的强大作用。 有序集合的数据和集合一样,不能重复,但每个元素又可以关联一个分数,这个分数可以重复。 需要注意的是,redis版本和命令变化较大,注意运行环境。 运行环境: redis 库版本:3.3.11 redis版本:3.2.1 1、从MongoDB中导入数据到Redis 生成数据: import pymongo import random handler = pymongo . MongoClient ( 'mongodb://127.0.0.1:27017' ) . chapter_9 . rank_data for i in range ( 10000 , 10100 ) : data = { 'user_id' : i , 'score' : round ( random . random ( ) * 1000 , 1 ) } handler . insert_one ( data ) 导入到有序集合中: import pymongo import redis handler = pymongo . MongoClient ( 'mongodb://127.0.0.1:27017' ) . chapter_9 . rank_data client = redis . Redis ( ) rows =

How do I copy a collection from one database to another database on the same server using PyMongo?

你说的曾经没有我的故事 提交于 2019-12-09 04:21:28
I am trying to copy a MongoDB collection from one database to another database on the same server. from pymongo import MongoClient client = MongoClient() client.db1.coll1.insert({'content':'hello world'}) I would like to copy db1.coll to db2.coll2 on the same server. I tried to follow the "copy and move" approach described in How to copy a collection from one database to another in MongoDB but this doesn't work in pymongo. In particular, the copy step client.db1.coll1.aggregate([{'$out':'coll2'}]) works but then I get an error when trying to perform the move: >>> client.admin.command({

Is there a way to store python objects directly in mongoDB without serializing them

丶灬走出姿态 提交于 2019-12-09 04:20:27
问题 I have read somewhere that you can store python objects (more specifically dictionaries) as binaries in MongoDB by using BSON. However right now I cannot find any any documentation related to this. Would anyone know how exactly this can be done? 回答1: There isn't a way to store an object in a file (database) without serializing it. If the data needs to move from one process to another process or to another server, it will need to be serialized in some form to be transmitted. Since you're

pymongo MongoClient connect to ReplicaSet

…衆ロ難τιáo~ 提交于 2019-12-08 20:30:06
问题 I adopted pymongo's MongoClient class to do connect to a replicaset which has three node, 1 primary 2 secondary. The code snippet as following: c = MongoClient([secondary1_hostname, secondary2_hostname], replicaSet='rs0') When check the three mongod's log, I found there is always a connection created to the primary host, but other 2 secondary not received the connection request from client or got connection immediately disconnected. Seems the client first reached one secondary got the primary

pymongo - mongodb+srv “dnspython must be installed” error

爷,独闯天下 提交于 2019-12-08 15:21:05
问题 I am trying to connect MongoDB from Atlas But I encountered: dnspython must be installed error My mongo uri (mockup): mongodb+srv://abc:123@something.something.com/admin?retryWrites=True My pymongo version : 3.6.1 I have installed dnspython and done import dns Still, I am getting the error: dnspython module must be installed to use mongodb+srv:// URI 回答1: In order to use mongo+srv protocol, you need to install pymongo-srv Launch this command to do it with python 3: pip3 install pymongo[srv]

pymongo - Unable to connect to mongodb running on EC2

不打扰是莪最后的温柔 提交于 2019-12-08 14:27:36
I am connecting to a mongodb server on EC2. The mongo collections require authentication to connect. I tried everything but I am getting the following error and can't seem to correct it. from pymongo import MongoClient mongo_username = "username" mongo_password = "password" ssh_user = "user" ssh_address = "ec2-**********.amazonaws.com" ssh_port = 22 private_key = "path/to/key/mykey.pem" def connect_to_mongo(): try: client = MongoClient("mongodb://"+mongo_username+":"+mongo_password+"@" + ssh_address, ssl = True, ssl_keyfile = private_key) db = client.myDB #Should 'admin' be there or 'myDB'?

pymongo.errors.OperationFailure: unsupported type in filter

吃可爱长大的小学妹 提交于 2019-12-08 13:54:29
问题 I am working on Project Django with MongoDB (djongo) and I am using mongo atlas But when I am running this command python3 manage.py runserver python3 manage.py makemigrations this show this type error pymongo.errors.OperationFailure: unsupported type in filter But if I use my local mongod then it's works fine. Please help me My requirements.txt dataclasses==0.6 dj-database-url==0.5.0 Django==2.2 django-cors-headers==3.0.1 djangorestframework==3.9.4 djongo==1.2.32 dnspython==1.16.0 pymongo==3