pymongo

How to Store different language(non english) data in MongoDB Field and retrive the same data?

好久不见. 提交于 2019-12-22 06:16:52
问题 I am tring to store non english(like: Bengali,Hindi) data in a MongoDB field. This is my approach:- import pymongo from pymongo import MongoClient client = MongoClient() db = client.testdb db['testing'].save({'data':'শুভ নববর্ষ'}) I got an Exception. Exception Value: Non-ASCII character '\xe0' in file /test/views.py on line 5, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details (views.py, line 5) After that I have tried like this:- from bson import BSON bson

add user to mongodb via python

流过昼夜 提交于 2019-12-22 04:39:22
问题 I want to be able to add users to MongoDB so that we can automate MongoDB installs with authentication already baked in. I can successfully add users using pymongo that are read only or are dbOwner by doing this: from pymongo import MongoClient client = MongoClient('localhost:27017') client.admin.authenticate('siteRootAdmin', 'Test123') client.testdb.add_user('newTestUser', 'Test123', True) but when I do this following code block to specify roles, it fails: from pymongo import MongoClient

Read-your-own-writes consistency in Mongodb

喜你入骨 提交于 2019-12-22 04:12:07
问题 first, here is what is said in Pymongo Documentation By default, PyMongo starts a request for each thread when the thread first runs an operation on MongoDB. This guarantees **read-your-writes consistency . Within a request, the thread will continue to use the same socket exclusively, and no other thread will use this socket, until the thread calls end_request() or it terminates. At that point, the socket is returned to the connection pool for use by other threads. so when using an async

how to use “group” in pymongo to group similar rows?

不想你离开。 提交于 2019-12-22 04:07:43
问题 I am very new to mongodb/pymongo. I have successfully imported my data into mongo and would like to use the group function to group similar row together. For example, if my data set looks like this: data = [{uid: 1 , event: 'a' , time: 1} , {uid: 1 , event: 'b' , time: 2} , {uid: 2 , event: 'c' , time: 2} , {uid: 3 , event: 'd' , time: 4} ] How do I use the group function to group the above rows according to the uid field such that the output is as follows? { {uid: 1} : [{uid: 1 , event: 'a'

close() never close connections in pymongo?

一笑奈何 提交于 2019-12-22 04:00:22
问题 I use MongoDB and I connect to it through pymongo. Here's my code: >>> import pymongo >>> con=pymongo.Connection('localhost',27017) >>> con.database_names() ['local', 'bookdb'] >>> con.close() >>> con.database_names() ['local', 'bookdb'] I use con.close() to disconnect to the MongoDB, but after that, I can still use con.database_names() to see the list of the databases. Why? it never disconnect to the MongoDB server. Why the close() not work? 回答1: Just read the docs, faster and more detailed.

MongoDB: Query a key having space in its name

眉间皱痕 提交于 2019-12-22 03:49:06
问题 I want to retrieve values of only certain keys from a MongoDB collection. But, the collection has some keys which have a 'space' in their name like: "Parent":{"key1": //some string, "key2": //some string, "key 3": //some string} I know this is a wrong approach as there shouldn't ideally be spaces in a key name but nevertheless how do I query this key? I am using Python and PyMongo. For normal keys I can do this: db.coll_name.find({"key": "India"}, {"_id": 0, "Parent.key1": 1, "Parent.key2": 1

Join and format array of objects in Python

假装没事ソ 提交于 2019-12-22 00:32:09
问题 I want to join and format values and array of objects to a string in python. Is there any way for me to do that? url = "https://google.com", search = "thai food", search_res = [ { "restaurant": "Siam Palace", "rating": "4.5" }, { "restaurant": "Bangkok Palace", "rating": "3.5" } ] url = "https://google.com", search = "indian food", search_res = [ { "restaurant": "Taj Palace", "rating": "2.5" }, { "restaurant": "Chennai Express", "rating": "5.0" } ] url = "https://bing.com", search = "thai

How to quickly fetch all documents MongoDB pymongo

萝らか妹 提交于 2019-12-21 12:19:12
问题 Currently I fetch documents by iterating through cursor in pymongo, for example: for d in db.docs.find(): mylist.append(d) For reference, performing a fetchall on the same set of data (7m records) takes around 20 seconds while the method above takes a few minutes. Is there a faster way read bulk data in mongo? Sorry I'm new to mongo, please let me know if more information is needed. 回答1: using the $natural sort will bypass the index and return the documents in the order in which they are

MongoDB之pymongo

只愿长相守 提交于 2019-12-21 11:36:36
PyMongo是什么  PyMongo是驱动程序,使python程序能够使用Mongodb数据库,使用python编写而成. 安装  环境:Ubuntu 14.04+python2.7+MongoDB 2.4 先去官网下载软件包,地址 点击打开链接 .解压缩后进入,使用 python setup.py install 进行安装 或者用pip安装 pip -m install pymongo 基本使用  创建连接 import pymongo client = pymongo.MongoClient( 'localhost', 27017)  或者可以这样 import pymongo client = MongoClient( 'mongodb://localhost:27017/') 连接数据库 db = client.mydb   或者 db = client[ 'mydb'] 连接聚集  聚集相当于关系型数据库中的表 collection = db.my_collection  或者 collection = db[ 'my_collection'] 查看数据库下所有聚集名称 db.collection_names() 插入记录 collection.insert({ "key1": "value1", "key2", "value2"}) 删除记录  全部删除

MongoDB与pymongo

别等时光非礼了梦想. 提交于 2019-12-21 10:39:44
首先,必须确认以下环境已经安装: 1. Python 2. MongoDB 3. pymongo 导入模块 import pymongo 使用MongoClient连接MongoDB from pymongo import MongoClient client = MongoClient(host, port) 引用MongoClient来创建数据库连接实例。在MongoDB中,默认的host和port如下: client = MongoClient('localhost', 27017) 或者使用MongoDB的URL来引用: client = MongoClient('mongodb://localhost:27017/') 连接MongoDB的一个数据库 任何一个连接实例都可以连接一个或者多个独立的数据库。这里默认已有一个名为test_db的数据库,下面是连接方法: database = client.test_database 或者当你的数据库名称不符合Python标准的时候,可以用: database = client['test-database'] 读取一个Collection Collection在这里的意思是一个存储在MongoDB中的文件集合,相当于关系型数据库中的table。具体方法和database一样: collection = db.test