pymongo

Pymongo find if value has a datatype of NumberLong

南笙酒味 提交于 2019-12-12 05:39:27
问题 I'm using the Pymongo driver and my documents look like this: { "_id" : ObjectId("5368a4d583bcaff3629bf412"), "book_id" : NumberLong(23302213), "serial_number" : '1122', } This works because the serial number is a string: find_one({"serial_number": "1122"}) However, this doesn't: find_one({"book_id": "23302213"}) Obviously its because the book_id has a datatype of NumberLong. How can execute the find method based on this datatype? ================================================== Update:

Pymongo aggregate: filter by count of fields number (dynamic)

徘徊边缘 提交于 2019-12-12 04:33:33
问题 Let's say I have an aggregation pipeline that for now leads to a collection with documents built like this: {'name': 'Paul', 'football_position': 'Keeper', 'basketball_position': 4,...} Obviously not everyone plays every sport so for some documents there would be fields that do not exist. The document regarding them would then be {'name': 'Louis'} What I want to do is to filter people that play at least one sport, inside my aggregation pipeline I know that this is easy to check for one field

specifying $push location in collection using Pymongo

被刻印的时光 ゝ 提交于 2019-12-12 04:16:53
问题 I'm wondering how to use $push in a collection where I have to specify which document I want to add information to. Please consider the following example: student1 = { 'name': 'Adam', 'year': 'sophomore', 'age': 20, 'class':[ { 'className': 'cse131', 'time': '2:30', 'finalGrade': 'A' }, { 'className': 'cse240', 'time': '9:30', 'finalGrade': 'B' } ] } If I wanted to update only the first document in the 'class' collection (the cse131 in 'class'), how would I use $push to do this? For example,

Tailing a collection before it is created

不羁的心 提交于 2019-12-12 03:29:44
问题 I'm developing an application where data is inserted into a capped collection via mongoose in Node.js and is tailed by pymongo in a Python program. I'm having a small issue where mongoose doesn't create the collection until a document is inserted into it. In Node.js I attempt to create the collection like so: var MyModel = mongoose.model('mymodel', mymodelSchema); In my pymongo program I want to tail MyModel so I do this: mymodels = db.mymodels.find(cursor_type=CursorType.TAILABLE_AWAIT) The

Flask RESTful API request, Broken pipe [Errno 32] !

夙愿已清 提交于 2019-12-12 03:27:08
问题 I'm new to web development and I'm trying to create a RESTful web service using the Flask micro-framework. Here is my code: app = Flask(__name__) client = MongoClient() db = client.markets def toJson(data): return json.dumps(data, default=json_util.default) @app.route('/', methods=['GET']) def get_tasks(): cursor = db.europe.find() list = [] for i in cursor: list.append(i) return toJson(list) When I send the request from my browser, it is constantly waiting for the server and nothing is

MongoDB - What is the fastest way to get the latest value as-of a given date?

天涯浪子 提交于 2019-12-12 03:16:50
问题 I have a collection of measurements from different sources, which come at different frequencies. How do I get the latest good value as-of a specific date , for any given subset of sources? (this is similar to pandas.Index.asof)? To be clear, for some of these timeseries there might be no available value for the desired date , so I must find the most recent among the available dates that are lower than the query date. The timeseries could look like this: {_id:new ObjectId(), source:

$all parameter in mongodb does not work with ObjectId list

痴心易碎 提交于 2019-12-12 03:15:44
问题 I retrieve a list of ObjectId and I want to retrieve all object in my mongo database using the parameter $all I'm using pymongo and my request look like this : db.database.collection.find({ "_id" : { "$all" : [ObjectId('4ee371837c93dd33dc000003'),ObjectId('4eef9f647c93dd1a90000000')] } }) but the cursor count returned by the request is 0 but when I do this request: db.database.collection.find_one({ "_id" : ObjectId('4ee371837c93dd33dc000003')}) It returns me the good object Anyone know why it

How do I fetch all mongo data which is created greater than a particular date?

爱⌒轻易说出口 提交于 2019-12-12 02:44:56
问题 I read the $gt attribute has to be used. Not able to get around this. Let's say I have some mongo data like this: { "startTime" : "Sun 25 Jan 2015 07:14:26 GMT", "endTime" : "", "jobStatus" : "JOBCANCELLED", "uiState" : "HISTORY", "priority" : "SILVER" } That's how my start time is saved in my Mongo. If I want to get the statuses of all jobs which have the start time greater than today, How do I do it? db.getCollection('jobsCollection').find({"startTime":{$gt: "What here?"}) 回答1: First you

Pymongo gives db assertion failure while trying to access remote server

人走茶凉 提交于 2019-12-12 02:03:26
问题 I am getting the following error: db assertion failure, assertion: 'unauthorized db:db1 lock type:-1 client:', assertionCode: 10057 I am able to access the MongoDB database by running python on the shell of my server. But when I try to access my site i get this unauthorised errors. Any fix for this error? 回答1: This means that your database is using authentication. In such a setup, you must authenticate a valid user before you can perform any operations (queries, commands, updates, etc). You

python convert object into json for mongodb

余生长醉 提交于 2019-12-12 02:00:06
问题 Folks, I have the following Class: class User(object): def __init__(self, name, bd, phone, address): self.name = name self.bd = bd self.phone = phone self.address = address myUser = User(name, bd, phone, address) Now I need to store myUser as an object in MongoDB. Should I use jsondumps for this? Whats the proper way of converting this object for pymongo? Thanks 回答1: While using an ORM is a good approach in general, depending on the complexity of your system, it might be simpler to do