pymongo-3.x

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',

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

Pymongo $in Query Not Working

不问归期 提交于 2019-12-05 03:42:41
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','lake strugeon']}},{_id:0}) Here is the result from shell { "SPECIES_ID" : 1, "SPECIES_AB" : "LKS",

How to dump a collection to json file using pymongo

不打扰是莪最后的温柔 提交于 2019-12-04 04:11:21
问题 I am trying to dump a collection to .json file but after looking in pymongo tutorial I can not find any thing that relates to it. Tutorial link: https://api.mongodb.com/python/current/tutorial.html 回答1: Just get all documents and save them to file e.g.: import json from pymongo import MongoClient if __name__ == '__main__': client = MongoClient() db = client.db_name collection = db.collection_name cursor = collection.find({}) file = open("collection.json", "w") file.write('[') for document in

Why is PyMongo count_documents is slower than count?

你。 提交于 2019-12-01 11:38:59
In db['TF'] I have about 60 million records. I need to get the quantity of the records. If I run db['TF'].count() , it returns at once. If I run db['TF'].count_documents({}) , that is a such long time before I get the result. However, the count method will be deprecated. So, how can I get the quantity quickly when using count_documents ? Is there some arguments I missed? I have read the doc and code, but nothing found. Thanks a lot! Amit Wagner This is not about PyMongo but Mongo itself. count is a native Mongo function. It doesn't really count all the documents. Whenever you insert or delete

Why is PyMongo count_documents is slower than count?

牧云@^-^@ 提交于 2019-12-01 09:03:07
问题 In db['TF'] I have about 60 million records. I need to get the quantity of the records. If I run db['TF'].count() , it returns at once. If I run db['TF'].count_documents({}) , that is a such long time before I get the result. However, the count method will be deprecated. So, how can I get the quantity quickly when using count_documents ? Is there some arguments I missed? I have read the doc and code, but nothing found. Thanks a lot! 回答1: This is not about PyMongo but Mongo itself. count is a

Bulk update in Pymongo using multiple ObjectId

ε祈祈猫儿з 提交于 2019-11-29 07:43:23
I want to update thousands of documents in mongo collection. I want to find them using ObjectId and then whichever document matches , should be updated. My update is same for all documents. I have list of ObjectId. For every ObjectId in list, mongo should find matching document and update "isBad" key of that document to "N" ids = [ObjectId('56ac9d3fa722f1029b75b128'), ObjectId('56ac8961a722f10249ad0ad1')] bulk = db.testdata.initialize_unordered_bulk_op() bulk.find( { '_id': ids} ).update( { '$set': { "isBad" : "N" } } ) print bulk.execute() This gives me result : {'nModified': 0, 'nUpserted':

How to escape @ in a password in pymongo connection?

纵然是瞬间 提交于 2019-11-29 01:24:52
My question is a specification of how can i validate username password for mongodb authentication through pymongo? . I'm trying to connect to a MongoDB instance using PyMongo 3.2.2 and a URL that contains the user and password, as explained in MongoDB Docs . The difference is that the password I'm using contains a '@'. At first I simply tried to connect without escaping, like this: prefix = 'mongodb://' user = 'user:passw_with_@_' suffix = '@127.0.0.1:27001/' conn = pymongo.MongoClient(prefix + user + suffix) Naturally I got the following error: InvalidURI: ':' or '@' characters in a username

Bulk update in Pymongo using multiple ObjectId

早过忘川 提交于 2019-11-28 01:13:25
问题 I want to update thousands of documents in mongo collection. I want to find them using ObjectId and then whichever document matches , should be updated. My update is same for all documents. I have list of ObjectId. For every ObjectId in list, mongo should find matching document and update "isBad" key of that document to "N" ids = [ObjectId('56ac9d3fa722f1029b75b128'), ObjectId('56ac8961a722f10249ad0ad1')] bulk = db.testdata.initialize_unordered_bulk_op() bulk.find( { '_id': ids} ).update( { '

How to escape @ in a password in pymongo connection?

我只是一个虾纸丫 提交于 2019-11-27 15:55:25
问题 My question is a specification of how can i validate username password for mongodb authentication through pymongo?. I'm trying to connect to a MongoDB instance using PyMongo 3.2.2 and a URL that contains the user and password, as explained in MongoDB Docs. The difference is that the password I'm using contains a '@'. At first I simply tried to connect without escaping, like this: prefix = 'mongodb://' user = 'user:passw_with_@_' suffix = '@127.0.0.1:27001/' conn = pymongo.MongoClient(prefix +