天道酬勤,学无止境

pymongo

Mongoengine PointField gives location object expected, location array not in correct format error

I have a model as follows: class Station(Document): location = PointField() Trying to write data as follows: station = Station() station.location = { "type": "Point", "coordinates": [ 81.4471435546875, 23.61432859499169 ] } station.save() However this gives the error Could not save document (location object expected, location array not in correct format) Mongoengine documentation says such a dictionary should be OK. What am I missing here?

2022-01-18 14:42:02    分类:问答    python   mongodb   pymongo   mongoengine   nosql

How can I tell if there are more results from a query in MongoDB?

Is there a preferred way to query mongo with a limit and know whether there will be more results if I query for the next page with skip/limit? What I have been doing is asking for one more document than I need, slicing it off the end, and using the existence of that extra document to know whether another query will give at least one more result. n = 10 docs = db.documents.find({'foo': 'bar'}).limit(n+1) more = docs.count() > n docs = docs[:n] I feel like this is a common use case (knowing whether or not to show an "more results" button on a website) and I feel stupid with my current solution.

2022-01-18 11:21:33    分类:问答    mongodb   pymongo

Find distinct documents with max value of a field in mongodb

I have thousands of documents in MongoDB with some of the sample as below: {"title":"Foo", "hash": "1234567890abcedf", "num_sold": 49, "created": "2013-03-09 00:00:00"} {"title":"Bar", "hash": "1234567890abcedf", "num_sold": 55, "created": "2013-03-11 00:00:00"} {"title":"Baz", "hash": "1234567890abcedf", "num_sold": 55, "created": "2013-03-10 00:00:00"} {"title":"Spam", "hash": "abcedef1234567890", "num_sold": 20, "created": "2013-03-11 00:00:00"} {"title":"Eggs", "hash": "abc1234567890def", "num_sold": 20, "created": "2013-03-11 00:00:00"} Is it possible to select all documents with distinct

2022-01-18 09:36:28    分类:问答    mongodb   pymongo

bson.errors.InvalidDocument: key '$oid' must not start with '$' trying to insert document with pymongo

I want to insert document to the collection from json file it says bson.errors.InvalidDocument: key '$oid' must not start with '$' How can I solve it? example of document: [{"name": "Company", "_id": {"$oid": "1234as123541gsdg"}, "info": {"email": "test@gmail.com"}}]

2022-01-17 22:00:29    分类:问答    python   pymongo

pymongo - Message length is larger than server max message size

the line for doc in collection.find({'is_timeline_valid': True}): is giving the Message Length error. How can I get all the collection without the error? I know about the find().limit() but I don't know how to use it. Code: from openpyxl import load_workbook import pymongo import os wb = load_workbook('concilia.xlsx') ws = wb.active client = pymongo.MongoClient('...') db = client['...'] collection = db['...'] r = 2 for doc in collection.find({'is_timeline_valid': True}): for dic in doc['timeline']['datas']: if 'concilia' in dic['tramite'].lower(): ws.cell(row = r, column = 1).value = doc['id

2022-01-17 04:40:54    分类:问答    python   mongodb   pymongo

检查 MongoDB 文档中是否存在多个字段(Check for existence of multiple fields in MongoDB document)

问题 我正在尝试查询一个数据库集合,该集合包含具有特定字段的那些文档的进程文档。 为简单起见,想象以下一般文档模式: { "timestamp": ISODate("..."), "result1": "pass", "result2": "fail" } 现在,当一个进程启动时,会插入一个新文档,其中仅包含时间戳。 当该过程达到某些阶段时,字段result1和result2会随着时间的推移而添加。 然而,一些过程没有达到阶段1或2 ,因此没有结果字段。 我想查询数据库以仅检索那些同时具有result1和result2的文档。 我知道 $exists 运算符,但据我所知,这一次只适用于一个字段,即db.coll.find({"result1": {$exists: true}}) 。 $exists运算符不能用作顶级运算符。 例如,这不起作用: db.coll.find({"$exists": {"result1": true, "result2": true}}) 要检查这两个结果,我需要: db.coll.find({"result1": {"$exists": true}, "result2": {"$exists": true}}) 现在,对于不止一个变量,这已经变得乏味了。 有一个更好的方法吗? (另外,我是在 Python 中执行此操作的,所以如果有一个仅针对

2022-01-16 15:16:59    分类:技术分享    python   mongodb   pymongo

Check for existence of multiple fields in MongoDB document

I am trying to query a database collection that holds documents of processes for those documents that have specific fields. For simplicity imagine the following general document schema: { "timestamp": ISODate("..."), "result1": "pass", "result2": "fail" } Now, when a process is started a new document is inserted with only the timestamp. When that process reaches certain stages the fields result1 and result2 are added over time. Some processes however do not reach the stages 1 or 2 and therefore have no result fields. I would like to query the database to retrieve only those documents, which

2022-01-16 09:13:21    分类:问答    python   mongodb   pymongo

在 Python 中加入和格式化对象数组(Join and format array of objects in Python)

问题 我想将值和对象数组连接并格式化为python中的字符串。 我有什么办法吗? 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 food", search_res = [ { "restaurant": "Siam Palace", "rating": "1.5" }, { "restaurant": "Bangkok Palace", "rating": "4.5" } ] url = "https://bing.com", search = "indian food"

2022-01-16 08:38:44    分类:技术分享    python   json   django   mongodb   pymongo

pymongo.errors.OperationFailure:过滤器中不支持的类型(pymongo.errors.OperationFailure: unsupported type in filter)

问题 我正在使用 MongoDB (djongo) 开发 Django 项目,我正在使用 mongo atlas 但是当我运行这个命令时 python3 manage.py runserver python3 manage.py makemigrations 这显示了这种类型的错误 pymongo.errors.OperationFailure: unsupported type in filter 但是,如果我使用我当地的 mongod 那么它工作正常。 请帮我 我的要求.txt dataclasses==0.6 dj-database-url==0.5.0 Django==2.2 django-cors-headers==3.0.1 djangorestframework==3.9.4 djongo==1.2.32 dnspython==1.16.0 pymongo==3.8.0 sqlparse==0.2.4 我的数据库设置.py DATABASES = { 'default': { 'ENGINE': 'djongo', 'NAME': 'leverrage', 'HOST': 'mongodb+srv://xxxxxxx:xxxxxxxx@cluster0-dqfd1.mongodb.net/test?ssl=true&ssl_cert_reqs=CERT_NONE

2022-01-16 03:11:28    分类:技术分享    django   mongodb   python-3.6   pymongo

遍历 PyMongo 游标会抛出 InvalidBSON: year is out of range(Iterating through PyMongo cursor throws InvalidBSON: year is out of range)

问题 我正在使用 PyMongo 简单地迭代 Mongo 集合,但我在处理大型 Mongodb 日期对象时遇到了困难。 例如,如果我的集合中有一些数据,如下所示: "bad_data" : [ { "id" : "id01", "label" : "bad_data", "value" : "exist", "type" : "String", "lastModified" : ISODate("2018-06-01T10:04:35.000Z"), "expires" : Date(9223372036854775000) } ] 我会做类似的事情: from pymongo import MongoClient, database, cursor, collection client = MongoClient('localhost') db = client['db1'] db.authenticate('user', 'pass', source='admin') collection = db['collection'] for i in collection: # do something with i 并得到错误InvalidBSON: year 292278994 is out of range 有什么方法可以处理这个可笑的Date()对象而不会摔倒? 我意识到在

2022-01-15 19:01:59    分类:技术分享    python   mongodb   mongodb-query   pymongo