pymongo

TypeError at /accounts/login/ id must be an instance of (str, unicode, ObjectId), not <type 'int'>

让人想犯罪 __ 提交于 2019-12-31 02:23:05
问题 This is my first example code for mongodb I am trying to connect my django with mongodb . I did my registration part successfully, But when I am trying to log in its showing an error page. TypeError at /accounts/login/ id must be an instance of (str, unicode, ObjectId), not type 'int' I am using django user authentication, this is my url url(r'^accounts/login/', 'django.contrib.auth.views.login'), below is my full traceback, TypeError at /admin/profile/ id must be an instance of (str, unicode

MongoDB aggregate/group/sum query translated to pymongo query

不想你离开。 提交于 2019-12-30 04:44:04
问题 I have a set of entries in the goals collection that looks like this: {"user": "adam", "position": "attacker", "goals": 8} {"user": "bart", "position": "midfielder", "goals": 3} {"user": "cedric", "position": "goalkeeper", "goals": 1} I want to calculate a sum of all goals. In MongoDB shell I do it like this: > db.goals.aggregate([{$group: {_id: null, total: {$sum: "$goals"}}}]) { "_id" : null, "total" : 12 } Now I want to do the same in Python using pymongo. I tried using both db.goals

Mongodb Query based on number of fields in a record

浪尽此生 提交于 2019-12-29 09:21:52
问题 I haven't been very good at Googling for this answer. I have around 115 different fields that might be in each record. Collection is the output of a mapreduce on an amazingly large dataset. Looks like this: {_id:'number1', value:{'a':1, 'b':2, 'f':5}}, {_id:'number2', value:{'e':2, 'f':114, 'h':12}}, {_id:'number3', value:{'i':2, 'j':22, 'z':12, 'za':111, 'zb':114}} Any ideas of how I might find records with 5 fields populated? 回答1: It's still not a nice query to run, but there is a slightly

How to sort sub-documents in the array field?

随声附和 提交于 2019-12-29 09:14:25
问题 I'm using the MongoDB shell to fetch some results, ordered. Here's a sampler, { "_id" : "32022", "topics" : [ { "weight" : 281.58551703724993, "words" : "some words" }, { "weight" : 286.6695125796183, "words" : "some more words" }, { "weight" : 289.8354232846977, "words" : "wowz even more wordz" }, { "weight" : 305.70093587160807, "words" : "WORDZ" }] } what I want to get is, same structure, but ordered by "topics" : [] { "_id" : "32022", "topics" : [ { "weight" : 305.70093587160807, "words"

pymongo: MongoClient or Connection

孤街醉人 提交于 2019-12-29 06:47:36
问题 I am trying to connect mongodb using pymongo. I see two classes to connect to mongodb. MongoClient and Connection. What is the difference of these two classes? 回答1: MongoClient is the preferred method of connecting to a mongo instance. The Connection class is deprecated. But, in terms of use they are very similar. 回答2: MongoClient and Connection are similar but MongoClient was introduced (since mongodb 2.2+ onwards) to mainly support WriteConcern and other features. Connection is depreciated,

importing JSON to mongoDB using pymongo

萝らか妹 提交于 2019-12-29 05:34:08
问题 i am trying to import a JSON file i pull from a URL and send it to mongoDB as is, using the pymongo module. I have the following code #!/usr/bin/env python import sys, urllib2, json, pymongo from pymongo import MongoClient myurl = "https://gist.githubusercontent.com/border/775526/raw/b921df18ba00262ab5bba8cadb3c178e1f7748f7/config.json" response = urllib2.urlopen(myurl) data = response.read() connection = MongoClient('mongodb://user:password@localhost.com:27017/database') connection.database

how to Iterate a mongo cursor in a loop in python

不羁岁月 提交于 2019-12-29 01:39:29
问题 I am trying to iterate through a loop in python but the nested loop is not reaching the incremental element. Is there any way other than using range like "hasNext()"? cursor1 = Collection.find({x : {"$gt" : 1}}) array1 = [] array2 = [] print Collection.count() for r in range(0, cursor1.count()): first = cursor1.next().get("entity") array2.append() for z in range(len(array2)): print len(original_tweets) if originalEntity.get("id") != duplicated_entity("id"): array2.append(second) 回答1: Just

How to get ordered dictionaries in pymongo?

∥☆過路亽.° 提交于 2019-12-28 02:10:45
问题 I am trying get ordered dictionaries in Pymongo. I have read it can be done with bson.son.Son. The Docs are Here However, I can't seem to make it work. There is not much on google about it. There are some discussions on configuring pymongo first to tell it to use SON objects but no examples. A friend suggested passing a param when you do a find. He couldn't remember. I'm able to create the SON objects. But when they get inserted into the DB and then come back out they are just plain dicts. I

pymongo authenticate failed连接mongodb数据库身份认证失败

天涯浪子 提交于 2019-12-26 17:12:56
问题:连接mongodb数据库身份认证失败。 尝试方法: 1.检查用户名、密码 2.检查IP、端口号 确认上述内容正确后,发现需要指定authSource才行。原因是公司数据库指定了这个数据库用上述凭据登录 pymongo文档 https://api.mongodb.com/python/current/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient 最终得到的链接代码 client = MongoClient( authSource='打码', username='name', password='pwd', host='1.1.1.1', port=27017, ) 来源: CSDN 作者: 日取其半万世不竭 链接: https://blog.csdn.net/jjiejingtazhu/article/details/103713889

Python操作MongoDb数据库

妖精的绣舞 提交于 2019-12-26 13:41:53
1.简介 MongoDB是一个基于分布式文件存储的文档数据库,可以说是非关系型(NoSQL,Not Only SQL)数据库中比较像关系型数据库的一个,具有免费、操作简单、面向文档、自动分片、可扩展性强、查询功能强大等特点,对大数据处理支持较好,旨在为WEB应用提供可扩展的高性能数据存储解决方案。 MongoDB将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB文档类似于JSON对象。字段值可以包含其他文档,数组及文档数组。 2.应用 MongoDB数据库可以到网站 https://www.mongodb.org/downloads 下载,安装之后打开命令提示符环境并切换到MongoDB安装目录中的server\3.2\bin文件夹,然后执行命令mongod --dbpath D:\data --journal -- storageEngine=mmapv1启动MongoDB,当然需要首先在D盘根目录下新建文件夹data。 让刚才那个命令提示符环境始终处于运行状态,然后再打开一个命令提示符环境,执行mongo命令连接MongoDB数据库,如果连接成功的话,会显示一个>符号作为提示符,之后就可以输入MongoDB命令了。 打开或创建数据库students >use students 在数据库中插入数据 >zhangsan = {‘name’: