pymongo

How to get distinct name and count in MongoDB using PyMongo

余生颓废 提交于 2020-06-17 09:34:12
问题 I have the below collection as shown below. All I want is the distinct "Name" and the count. For example Betty appears 2 times, so the output I want is Betty:2, Vic:1, Veronica:2. I am able to get the distinct Name by issuing the command " db.Car.find().distinct('Name') " but not sure how to get the count. { "Name": "Betty", "Car": "Jeep", } { "Name": "Betty", "Car": "Van", } { "Name": "Vic", "Car": "Ferrari", } { "Name": "Veronica", "Car": "Bus", } { "Name": "Veronica", "Car": "Van", } 回答1:

insert array into mongodb using pymongo

孤者浪人 提交于 2020-06-16 17:29:25
问题 I am trying to add array into mongdb using pymongo I have another program that will return something like ['1 aksdfjas;dkfjsa;dfkj','2 ;alksdjf;askdjf;asdfjkasdf', '3 ;alksdfj;asdlkfj;asdfj'] and I want to add them into the insert. 1)I cannot think of any other ways to do it so I am converting them to string and concatenate and trying to add them to the post(there must be better way no?) 2)When I do this, instead of desire affect, I get ["'1 aksdfjas;dkfjsa;dfkj','2 ;alksdjf;askdjf;asdfjkasdf

Django with mongodb using pymongo without using ORM

本秂侑毒 提交于 2020-06-11 10:39:25
问题 I just started working on a project, I am using Django1.10, I wanted to use mongoDB as backend... I tried all possible ways, django-mongo-engine requires django-nonrel1.5, but if I used it then I have to do lot of work, and its complicated... I tried django-mongoengine also but it was only tested on django1.9, (django 1.9 not supporting admin ) So now I decided to use pymongo ... I need help How can I configure database? and How to work with django without ORM? EDIT : This is my setting.py

Pymongo, query on list field, and/or

眉间皱痕 提交于 2020-06-07 13:45:14
问题 I have a collection with some documents like: { _id: 5, vals: [100, 1100, 1500] }, { _id: 10, vals: [1100, 1700] } How can I query for documents that have, in vals field: 1100 1700 OR 100 100 AND 1100 I can use some comprehension magic like: g = lambda codes: ( d for d in collection.find() if any(code in d["vals"] for code in codes) ) g([100, 1700]).next() Or, for the AND: g = lambda codes: ( d for d in collection.find() if all(code in d["vals"] for code in codes) ) g([100, 1100]).next() This

Querying MongoDB (via pymongo) in case insensitive efficiently

限于喜欢 提交于 2020-05-24 21:09:04
问题 I'm currently creating a website in python (pyramid) which requires users to sign up and log in. The system allows for users to choose a username which can be a mixture of capital letters, lowercase letters, and numbers. The problem arises when making sure that two users don't accidentally share the same username, i.e. in my system 'randomUser' should be the same as 'RandomUser' or 'randomuser'. Unfortunately (in this case) because Mongo stores strings as case sensitive, there could

Aggregating 3 MongoDB collections - (Python, Flask, Jinja)

时光总嘲笑我的痴心妄想 提交于 2020-05-16 02:22:53
问题 Thanks to great help found here I managed to put in place some aggregation for 2 collections in MongoDB. @app.route("/perfume/<id>", methods=["POST", "GET"]) def perfume(id): perfume = mongo.db.perfumes.find_one({"_id": ObjectId(id)}) form = AddReviewForm() cur = mongo.db.perfumes.aggregate( [ { "$lookup": { "from": "users", "localField": "author", "foreignField": "username", "as": "creator", } }, {"$unwind": "$creator"}, { "$project": { "_id": "$_id", "perfumeName": "$name", "perfumeBrand":

Aggregating 3 MongoDB collections - (Python, Flask, Jinja)

醉酒当歌 提交于 2020-05-16 02:21:09
问题 Thanks to great help found here I managed to put in place some aggregation for 2 collections in MongoDB. @app.route("/perfume/<id>", methods=["POST", "GET"]) def perfume(id): perfume = mongo.db.perfumes.find_one({"_id": ObjectId(id)}) form = AddReviewForm() cur = mongo.db.perfumes.aggregate( [ { "$lookup": { "from": "users", "localField": "author", "foreignField": "username", "as": "creator", } }, {"$unwind": "$creator"}, { "$project": { "_id": "$_id", "perfumeName": "$name", "perfumeBrand":

Filter nested array with conditions based on multi-level object values and update them - MongoDB aggregate + update

二次信任 提交于 2020-05-14 02:27:05
问题 Considering I have the following documents in a collection (ignoring the _id ) : [ { "Id": "OP01", "Sessions": [ { "Id": "Session01", "Conversations": [ { "Id": "Conversation01", "Messages": [ { "Id": "Message01", "Status": "read", "Direction": "inbound" }, { "Id": "Message02", "Status": "delivered", "Direction": "internal" }, { "Id": "Message03", "Status": "delivered", "Direction": "inbound" }, { "Id": "Message04", "Status": "sent", "Direction": "outbound" } ] }, { "Id": "Conversation02",

爬虫(九):python操作MySQL、MongoDB

谁说我不能喝 提交于 2020-05-07 23:17:12
1. python操作MySQL 1.1 MySQL基础 在java基础部分就写过了。 https://www.cnblogs.com/liuhui0308/p/11891844.html 1.2 pymysql模块 我在python基础部分已经写过pymysql了。 https://www.cnblogs.com/liuhui0308/p/11892199.html 2. python操作MongoDB 2.1 MongoDB基础 MongoDB(一):NoSQL简介、MongoDB简介 MongoDB(二):在Windows环境安装MongoDB MongoDB(三):数据库操作、集合操作 MongoDB(四):数据类型、插入文档、查询文档 MongoDB(五):更新文档、删除文档 MongoDB(六):选择字段、限制记录数、排序记录 MongoDB(七):聚合aggregate MongoDB(八):索引 2.2 pymongo模块 2.2.1 安装pymongo pip install pymongo -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com 2.2.2 连接MongoDB import pymongo # 获取连接的对象,两种连接方式都可以 client = pymongo

MongoDB之python简单交互(三)

不想你离开。 提交于 2020-05-07 22:08:44
python连接mongodb有多种orm,主流的有pymongo和mongoengine。 pymongo 安装相关模块 pip install pymongo pymongo操作 主要对象 MongoClient对象:用于与MongoDB服务器建立连接 DataBase对象:对应着MongoDB中的数据库 Collection对象:对应着MongoDB中的集合 Cursor对象:查询方法find()返回的对象,用于进行多行数据的遍历 主要方法 insert_one:加入一条文档对象 insert_many:加入多条文档对象 find_one:查找一条文档对象 find:查找多条文档对象 update_one:更新一条文档对象 update_many:更新多条文档对象 delete_one:删除一条文档对象 delete_many:删除多条文档对象 交互实例 # test.py from pymongo import * def get_col(): """获取一个集合对象""" try: # 连接mongodb,connect=False参数加上 client = MongoClient(host='192.168.1.21',port=27017, username="admin", password="123", connect=False) # client =