What is the proper way to check if a document in mongodb with find().limit()?

Manh Nguyen Huu
for hashtag in hashtags: 
    existing_hashtag = hashtags_collection.find({"string": hashtag}).limit(1)
    if existing_hashtag:
        hashtags_collection.update({"string": hashtag}, 
                                   {"$inc": {"popularity": 1}})                                                      
    else:
        new_hashtag = {"string": hashtag,
                       "popularity": 1}
        hashtags_collection.insert_one(new_hashtag)

find_one will return actual object but I heard that it is not efficient. find + limit only returns a cursor object even if it doesn't find a match. So how can I implement find + limit in mongodb?

styvane

First of all, don't issue a query for each element in your iterable here "hashtags" instead, you should use the $in query operator.

That being said, you can use the count method to check whether or not any document in your collection "string"'s value is your array.

collection.count({"string": {"$in": hashtags}})

Last and not least, you don't need the if/else statement here, simply let MongoDB do the job for you by using bulk operation and the upsert option.

In conclusion you code should look like this.

from pymongo import UpdateOne


bulk_operations = [UpdateOne({'string': value}, {'$inc': {'popularity': 1 }}, upsert=True) 
                   for value in hashtags]  
hashtags_collection.bulk_write(bulk_operations)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Proper way to check a property of Mongoose Document is empty

What's the proper way to document callbacks with jsdoc?

What is the proper way to document a CMake module?

What's the proper way to check if a constant is defined?

What is the proper way to check for null values?

What is the proper way to check if an Iterator is complete?

What is the proper way of combining 2 documents in MongoDB

what is the proper way to use $nin operator with mongoDB

What is the proper way of saving data to an embedded document using Mongoengine and Pyramid?

What is the proper way to format this txt document with csv writer and pandas?

What is the proper way to check if a Boolean key exists in NSUserdefaults/UserDefaults

What is the proper way to delay a service worker update check?

What is the proper way to check for existence of variable in an EJS template (using ExpressJS)?

What is the proper way to check for a foreign key with Rails 5.2.4 / rspec 3.12?

What's the proper way to check if JSON has a key?

Multiple check for "None" - what would be the proper way in Python

What is the proper way to check string or list of strings length in JS?

What is the proper way to check multiple form values from the template in Angular?

why mongodb find limit doesn't limit any document in the result set

Proper way to check for URL equality

What is the best practice to check if document with field value exists in MongoDB?

in mongodb, what is the most efficient way to get the first and last document

What is the fastest way to update the whole document (all fields) in MongoDB?

MongoDB limit find results

Check if document exists in mongodb

Is there a good way to find proper index?

MongoDB limit number of document to cycle?

What is the proper way to wait for connections?

What is the proper way to implement CupertinoActionSheet?

TOP Ranking

HotTag

Archive