pymongo

Find substrings in PyMongo

情到浓时终转凉″ 提交于 2019-12-07 01:27:36
问题 I wanted to find sub-strings within a field in MongoDB using PyMongo. The following query works fine and is what I require: db.collection.find({ "Animal": /cat|Dog/i}) However, if I try passing the value /cat|Dog/i as a string in Python, it doesn't work. Is there a way to replicate the query in PyMongo ? Note: /cat|Dog/i is value of field from another collection. It is in the form 'cat Dog'. Basically, I want to match substrings in one field with substrings in another. 回答1: You need to

How to join MongoDB collections in Python?

…衆ロ難τιáo~ 提交于 2019-12-07 01:16:52
问题 How to join ( in a sense of INNER JOIN from SQL ) two MongoDB collections in Python ? Do I need to use native map/reduce javascript code or to do this in PyMongo ? How to solve this with less code ? 回答1: Mongo stores data differently than in a traditional relational database, and does not support table joins as one might be used to in a SQL database. There is a note on this in the "Database References" documentation. http://www.mongodb.org/display/DOCS/Database+References If possible, it is

Encoding custom python objects as BSON with pymongo

旧城冷巷雨未停 提交于 2019-12-07 00:15:05
问题 Is there a way to tell pymongo to use a custom encoder to convert python objects to BSON? Specifically I need to convert numpy arrays into BSON. I know I can manually ensure every numpy array gets converted to a native python array before sending it to pymongo. But this is repetitive and error-prone. I'd much rather have a way to set up my pymongo connection to do this automatically. 回答1: You need to write a SONManipulator . From the docs: SONManipulator instances allow you to specify

Pymongo $in Query Not Working

旧城冷巷雨未停 提交于 2019-12-06 21:22:34
问题 Seeing some strange behavior in Pymongo $in query. Looking for records that meet the following query: speciesCollection.find({"SPCOMNAME":{"$in":['paddlefish','lake sturgeon']}}) The query returns no records. If I change it to find_one the it works returning the last value for Lake Sturgeon. The field is a text filed with one vaule. So I am looking for records that match paddlefish or Lake Sturgeon. It works fine in Mongo Shell like this: speciesCollection.find({SPCOMNAME:{$in: ['paddlefish',

Pymongo cursor limit(1) returns more than 1 result

随声附和 提交于 2019-12-06 19:46:14
问题 These are all documents in my collection: { "_id" : ObjectId("5110291e6ee1c31d5b275d01"), "d" : 24, "s" : [ 1, 2, 3 ] } { "_id" : ObjectId("511029266ee1c31d5b275d02"), "d" : 24, "s" : [ 4, 5, 6 ] } { "_id" : ObjectId("5110292e6ee1c31d5b275d03"), "d" : 24, "s" : [ 7, 8 ] } This the query I want to run: mongo = get_collection(self.collection_name) res = mongo.find().sort([('_id', -1)]).skip(1).limit(1) get_collection() is a helper method that I've made. Iterating over the cursor, res , produces

How to add a custom type to dill's pickleable types

主宰稳场 提交于 2019-12-06 15:52:45
I'm trying to serialize some code I did not write and cannot modify that needs to be pickled/dilled. The script contains a mongodb collection object---it isn't actually used later, but dilling it is throwing an error. When I try dilling it, I receive the error: Collection object is not callable. If you meant to call __getnewargs__ method on a 'Database' object it is failing because no such method exists. I see code here that is enumerating the accepted types: https://github.com/uqfoundation/dill/blob/master/dill/_objects.py (lines 132-190) and my suspicion is this is where I might change

单例模式

我与影子孤独终老i 提交于 2019-12-06 13:12:38
装饰器实现 # singleton.py import pymongo from functools import wraps def singleton(cls): _instance = {} @wraps(cls) def get_instance(*args, **kwargs): if cls not in _instance: _instance[cls] = cls( *args, **kwargs) return _instance[cls] return get_instance @singleton class MyMongoClient(pymongo.MongoClient): def __str__(self): return ('this is a 单例模式的应用') if __name__ == '__main__': cli = MyMongoClient() print(cli) cli2 = MyMongoClient() print( cli is cli2) 代码演示如下: (py3) jfxu@iZbp1gmf2s484lvojwrs4dZ:~$ python singleton.py this is a 单例模式的应用 True __ new __ 方法实现 # singleton.py import pymongo class

MongoDB Query - limit fields where name matches pattern

徘徊边缘 提交于 2019-12-06 11:12:13
I've read everything I can find about Projection in MongoDB. I'm hoping this is simple and I just missed it due to the overwhelming flexibility of Mongo queries. In our MySql database, we've adopted a business practice of having "hidden" fields be prefixed with an underscore. Our application knows how to hide these fields. Moving some data to mongo, I need to retrieve the documents, with ALL underscore prefixed fields omitted . Of course this should be done in the query rather than document manipulation after retrieval. All the operators like $regex, $in, $all seem to apply to values . I need

mongodb wildcard match all values for specific key [duplicate]

风流意气都作罢 提交于 2019-12-06 10:39:50
This question already has answers here : Check that Field Exists with MongoDB (4 answers) Closed 8 months ago . I am trying to figure out how to match a key and return all the values for that key. Is it possible to give the value as a wildcard? I want to return everything for that specific key using wildcard on the value. db.collection.find({"key" :"*"}) Also I was hoping this would return the entire collection as well that had the key with the wildcard value match as well. You may be looking for something like this: db.collection.find({"key": {$exists: true}}) This will return all documents

Mongoengine, retriving only some of a MapField

十年热恋 提交于 2019-12-06 09:27:33
For Example.. In Mongodb.. > db.test.findOne({}, {'mapField.FREE':1}) { "_id" : ObjectId("4fb7b248c450190a2000006a"), "mapField" : { "BOXFLUX" : { "a" : "f", } } } The 'mapField' field is made of MapField of Mongoengine. and 'mapField' field has a log of key and data.. but I just retrieved only 'BOXFLUX'.. this query is not working in MongoEngine.... for example.. BoxfluxDocument.objects( ~~ querying ~~ ).only('mapField.BOXFLUX') AS you can see.. only('mapField.BOXFLUX') or only only('mapField__BOXFLUX') does not work. it retrieves all 'mapField' data, including 'BOXFLUX' one.. How can I