pymongo

Multiple $where queries in MongoDB

本秂侑毒 提交于 2019-12-20 07:30:06
问题 I tried to put following query together, but it is not working: db.sss.find({ "pos": { "$gte": 200000, "$lt": 2000000 }, "$where": "(this.chr.letter != "X" && this.chr.no == 5) && (this.chr.letter != "X" && this.chr.no == 6) && (this.chr.letter != this.chr.letter)" }) The above condition above I tried to explain below: chr.no = 5 and chr.no = 6 chr.letter between two objects/dicts in chr are not the same and no X , and the document must be in range 200000 - 2000000 The example output could

PyMongo Update document with multiple records

别来无恙 提交于 2019-12-20 07:29:51
问题 I have a dictionary that looks like this: { "username": "SHAURYA", "stocks": [{ "name": "WXYZ", "count": 2, "price": 100 }, { "name": "GOOG", "count": 3, "price": 300 }, { "name": "QQV", "count": 5, "price": 300 }, { "name": "AAPL", "count": 6, "price": 300 }, { "name": "SN", "count": 4, "price": 300 }] } I need to be able to update individual stocks as well as add new stocks to this. If i use the db.cmpe285.update({"username":username}, {"$push": {"stocks":{"name":stock_symbol,"count"

Python 使用pymongo操作mongodb库

痴心易碎 提交于 2019-12-20 07:15:54
1,安装python3.5 如果python还没有安装,可以直接用yum安装, [python] view plain copy # 不过安装的是2.6 version yum install -y python 源码安装3.5 [python] view plain copy wget https://www.python.org/ftp/python/ 3.5. 0/Python- 3.5. 0.tgz tar -xvf Python- 3.5. 0.tgz cd Python- 3.5. 0 ./configure --prefix=/usr/local--enable-shared make make install ln -s /usr/local/bin/python3 /usr/bin/python3 运行Python之前需要配置库 echo /usr/local/lib >> /etc/ld.so.conf.d/local.conf ldconfig 运行演示 python3 --version 部分执行过程: [python] view plain copy [root@ 03_sdwm Python- 3.5. 0] # echo/usr/local/lib >> /etc/ld.so.conf.d/local.conf [root@ 03_sdwm

MongoClient opened before fork. Create MongoClient only Flask

拥有回忆 提交于 2019-12-20 05:12:59
问题 I am running Flask with uwsgi threaded mode with processes 4 and using pymongo also flask_mongoengine and uwsgi says "MongoClient opened before fork. Create MongoClient only " I tried connect with connect=False but the result is same lazy-apps = true problem is fixed but it seems that uwsgi needs more time to load what can be done for best performance ? 回答1: app.config['MONGODB_SETTINGS'] = {'DB': 'somedb', "USERNAME": "dbadmin", "PASSWORD":"somepass",'connect': False} And client =

MongoClient opened before fork. Create MongoClient only Flask

半世苍凉 提交于 2019-12-20 05:12:45
问题 I am running Flask with uwsgi threaded mode with processes 4 and using pymongo also flask_mongoengine and uwsgi says "MongoClient opened before fork. Create MongoClient only " I tried connect with connect=False but the result is same lazy-apps = true problem is fixed but it seems that uwsgi needs more time to load what can be done for best performance ? 回答1: app.config['MONGODB_SETTINGS'] = {'DB': 'somedb', "USERNAME": "dbadmin", "PASSWORD":"somepass",'connect': False} And client =

pymongo 基本操作

爱⌒轻易说出口 提交于 2019-12-20 04:54:17
from pymongo import * # 导包 con = Connection(...) # 链接 db = con.database # 链接数据库 db.authenticate('username', 'password') # 登录 db.drop_collection('users') #删除表 db.logout() # 退出 db.collection_names() # 查看所有表 db.users.count() # 查询数量 db.users.find_one({'name' : 'xiaoming'}) # 单个对象 db.users.find({'age' : 18}) # 所有对象 db.users.find({'id':64}, {'age':1,'_id':0}) # 返回一些字段 默认_id总是返回的 0不返回 1返回 db.users.find({}).sort({'age': 1}) # 排序 db.users.find({}).skip(2).limit(5) # 切片 db.users.find({}, {}, 10, 20) # 第二种写法 切片 未测试 collection 属性:full_name,name,database insert(self, doc_or_docs, manipulate=True, safe

Using maxTimeMS parameter with aggregation queries on Mongo 2.6 and Pymongo 2.7.1

一曲冷凌霜 提交于 2019-12-20 04:53:38
问题 I'm unable to use maxTimeMS parameter with Mongo 2.6 and Pymongo 2.7.1 As per the documentation on this page Official Mongodb Aggregation Page the aggregation method should return a Cursor object. However, when I run the query locally on a mongod instance (2.6+) with pymongo 2.7.1, I get a dict object! In [14]: obj = coll.aggregate({'$group': {'_id': '$l', 'n': {'$sum': 1}}}) In [15]: type(obj) Out[15]: dict Can anyone help me understand what is happening here? 回答1: Yes, you can use maxTimeMS

Using maxTimeMS parameter with aggregation queries on Mongo 2.6 and Pymongo 2.7.1

半城伤御伤魂 提交于 2019-12-20 04:53:04
问题 I'm unable to use maxTimeMS parameter with Mongo 2.6 and Pymongo 2.7.1 As per the documentation on this page Official Mongodb Aggregation Page the aggregation method should return a Cursor object. However, when I run the query locally on a mongod instance (2.6+) with pymongo 2.7.1, I get a dict object! In [14]: obj = coll.aggregate({'$group': {'_id': '$l', 'n': {'$sum': 1}}}) In [15]: type(obj) Out[15]: dict Can anyone help me understand what is happening here? 回答1: Yes, you can use maxTimeMS

MongoEngine specify read preference on query

青春壹個敷衍的年華 提交于 2019-12-20 03:11:00
问题 I am using Mongo 2.6, Pymongo 2.7.2 and Mongoengine 0.8.7. For a particular read query, I want to use the secondary of my replica set. Hence, as specified in the mongoengine documentation here I wrote my query as follows : from pymongo.read_preferences import ReadPreference <collection_name>.objects().read_preference(ReadPreference.SECONDARY_PREFERRED) However, the query is always going to the primary it seems ( The logs for this query are always seen only in the primary ). Is the syntax

PyMongo $inc having issues

不打扰是莪最后的温柔 提交于 2019-12-19 09:19:28
问题 When trying to use $inc I get a Syntax error stating that $set is wrong. My query is as follow: mongo_db.campaign.update({'_id': str(campaign_id)}, { $inc: { 'item': 1 } }): I've looked at the mongo documentation (http://docs.mongodb.org/manual/reference/operator/inc/) and other examples on SO but I can't find what's wrong. Any suggestions? Thanks! 回答1: $inc isn't a valid Python identifier. You should pass it as a string, like everything else: mongo_db.campaign.update({'_id': str(campaign_id)