pymongo

Flask-PyMongo - init_app() missing 1 required positional argument: 'app'

假如想象 提交于 2019-12-26 05:39:13
问题 i am trying to initialise my db using flask-pymongo. But i get the following error, File "run.py", line 22, in app = create_app("config") File "run.py", line 11, in create_app mongo.init_app(app) TypeError: init_app() missing 1 required positional argument: 'app' run.py from flask import Flask from app import api_bp from db import mongo def create_app(config_filename): app = Flask(__name__) app.config.from_object(config_filename) app.register_blueprint(api_bp, url_prefix='/api') mongo.init

How to query with multiple conditions and those conditions are dependent

淺唱寂寞╮ 提交于 2019-12-25 18:41:52
问题 Suppose "A001", "A002", "A003" in symptoms_N means "nose allergic" Suppose "Z001", "Z002" in symptoms_N means "nose cancer" I want to find those who got nose cancer AND the had got nose allergic before getting cancer. For example, the following 2 records hit the target I want. I can inferred Jack got "nose cancer" on 2015-04-02, and he had got "nose allergic" on 2011-04-02. I can find the nose allergic records with $or aggregation operator. like db.collection.find({"$or": OR_CONDITIONS}) I

Pymongo importing succeeded but not showing in the collection

筅森魡賤 提交于 2019-12-25 10:02:10
问题 I tried importing json file which succeeded mongoimport --db dbwhy --collection dbcol --jsonArray consumer_complaint.json 2016-01-15T19:00:42.277-0600 connected to: localhost 2016-01-15T19:00:42.320-0600 imported 34 documents but when I tried viewing it, it was not there from pymongo import MongoClient client = MongoClient('localhost',27017) db = client['dbwhy'] coll = db['dbcol'] curs = db.coll.find() for i in curs: print(i) It does not show anything 回答1: The problem is here: db.coll.find()

如何用“ like”查询MongoDB?

混江龙づ霸主 提交于 2019-12-25 09:42:10
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 我想查询一些与SQL的 like 查询: SELECT * FROM users WHERE name LIKE '%m%' 我如何在MongoDB中实现相同目标? 我在 文档中 找不到 like 的运算符。 #1楼 在 使用 Python的 PyMongo 猫鼬 使用 Node.js Jongo ,使用 Java MGO, 用 围棋 你可以做: db.users.find({'name': {'$regex': 'sometext'}}) #2楼 您可以使用where语句来构建任何JS脚本: db.myCollection.find( { $where: "this.name.toLowerCase().indexOf('m') >= 0" } ); 参考: http : //docs.mongodb.org/manual/reference/operator/where/ #3楼 如果使用 node.js , 则 表示您可以编写以下代码: db.collection.find( { field: /acme.*corp/i } ); //or db.collection.find( { field: { $regex: 'acme.*corp', $options: 'i' } } ); 另外

What is fastest structure to read in MongoDB: multiple documents or subdocuments?

北城以北 提交于 2019-12-25 07:05:38
问题 Intro I use Mongo to store moderately long financial timeseries, which I can read in 2 ways: retrieve 1 series for its entire length retrieve N series on a specific date To facilitate the second type of query, I slice the series by year. This reduces the data load when querying for large number of series on a specific day (example: if I query the value of 1000 timeseries on a specific day, it is not feasible to query back the entire history of each, which can go back 40 years = 28k each)

Using $push with $group with pymongo

自作多情 提交于 2019-12-25 04:12:11
问题 Objective Fix my make_pipeline() function to, using an aggregation query, count the number of tweets for each user, add them to an array and return the 5 users with the most tweets. Exercise Using an aggregation query, count the number of tweets for each user. In the same $group stage, use $push to accumulate all the tweet texts for each user. Limit your output to the 5 users with the most tweets. Your result documents should include only the fields: "_id" (screen name of user), "count"

In MongoDB, can I project whether a value has a substring to a new field?

梦想的初衷 提交于 2019-12-25 03:21:48
问题 In MongoDB, I want to group my documents based on whether a certain field has a certain substring. I was trying to project each document to a boolean that says whether the field has that substring/matches that pattern, and then group by that field. How can I do this? Edit: 1) I have tried the aggregation pipeline as db.aggregate([ { '$match': { '$regex': '.*world' } } ]) But I get an error: pymongo.errors.OperationFailure: command SON([('aggregate', u'test'), ('pipeline', [{'$match': {'$regex

mongoose vs pymongo drivers - write (insertMany) test

若如初见. 提交于 2019-12-25 02:34:41
问题 Benchmark with python import json import os from datetime import datetime import pymongo from bson import ObjectId jsons = [] DB_URL = os.environ.get('DB_URL') FILE_PATH = os.environ.get('FILE_PATH') client = pymongo.MongoClient(DB_URL) database = client[client.get_database().name] collection_name = 'test_collection' database[collection_name].drop() database[collection_name].create_index([('p.k', pymongo.ASCENDING), ('p.v', pymongo.ASCENDING)], background=True) def writeJsons(): global jsons

How to iterate through every other document from a mongo db cursor

十年热恋 提交于 2019-12-25 01:44:20
问题 I have a mongo DB cursor with documents that I want to create into Dataframes. However, the documents in that cursor can have a runTime that's too close. Therefore I'd like to get every other document and make a dataframe out of those. Attempt 1. all_df_forecast = [] for doc in cursor[::2]: single_fc_df = pd.DataFrame(doc['data']['PRICES SPOT']) all_df_forecast.append(single_fc_df) Results in: IndexError: Cursor instances do not support slice steps Attempt 2. all_df_forecast = [] for doc in

MongoDB & PyMongo: how to store a set / list?

戏子无情 提交于 2019-12-25 01:39:11
问题 Apparently it is not possible and the best advice I found so far is to use dict but this is not what I want. Is there still a way to store a set / list in MongoDB? 回答1: With mongo you have to pass a json (which in python we can essentially think of as a dict) in order to do anything really, so say you want to add a list or set of numbers to the hamburgers field in your collection, you would prepare it as follows: db.your_collection.update({'$push': {'hamburgers': {$each: [1, 2, 3, 4]}}}) if