如果集合存在且存在为空(如何从集合中删除),如何检入PyMongo?

时间:2022-03-25 02:59:51

How to check in PyMongo if collection exists and if exists empty (remove all from collection)? I have tried like

如果集合存在且存在为空(如何从集合中删除),如何检入PyMongo?我试过像

collection.remove()

or

collection.remove({})

but it doesn't delete collection. How to do that ?

但它不会删除集合。怎么做 ?

3 个解决方案

#1


46  

Sample code in Pymongo with comment as explanation:

Pymongo中的示例代码,注释为:

from pymongo import MongoClient
connection = MongoClient('localhost', 27017) #Connect to mongodb

print(connection.database_names())  #Return a list of db, equal to: > show dbs

db = connection['testdb1']          #equal to: > use testdb1
print(db.collection_names())        #Return a list of collections in 'testdb1'
print("posts" in db.collection_names())     #Check if collection "posts" 
                                            #  exists in db (testdb1)

collection = db['posts']
print(collection.count() == 0)    #Check if collection named 'posts' is empty

collection.drop()                 #Delete(drop) collection named 'posts' from db

#2


15  

You should use .drop() instead of .remove(), see documentation for detail: http://api.mongodb.org/python/current/api/pymongo/collection.html#pymongo.collection.Collection.drop

你应该使用.drop()而不是.remove(),详见文档:http://api.mongodb.org/python/current/api/pymongo/collection.html#pymongo.collection.Collection.drop

=====

Sorry for misunderstanding your question.

很抱歉误解了你的问题。

To check if a collection exists, use method collection_names on database:

要检查集合是否存在,请在数据库上使用方法collection_names:

>>> collection_name in database.collection_names()

To check if a collection is empty, use:

要检查集合是否为空,请使用:

>>> collection.count() == 0

both will return True or False in result.

两者都会在结果中返回True或False。

#3


5  

Have you tried this:

你试过这个:

db.collection.remove();

#1


46  

Sample code in Pymongo with comment as explanation:

Pymongo中的示例代码,注释为:

from pymongo import MongoClient
connection = MongoClient('localhost', 27017) #Connect to mongodb

print(connection.database_names())  #Return a list of db, equal to: > show dbs

db = connection['testdb1']          #equal to: > use testdb1
print(db.collection_names())        #Return a list of collections in 'testdb1'
print("posts" in db.collection_names())     #Check if collection "posts" 
                                            #  exists in db (testdb1)

collection = db['posts']
print(collection.count() == 0)    #Check if collection named 'posts' is empty

collection.drop()                 #Delete(drop) collection named 'posts' from db

#2


15  

You should use .drop() instead of .remove(), see documentation for detail: http://api.mongodb.org/python/current/api/pymongo/collection.html#pymongo.collection.Collection.drop

你应该使用.drop()而不是.remove(),详见文档:http://api.mongodb.org/python/current/api/pymongo/collection.html#pymongo.collection.Collection.drop

=====

Sorry for misunderstanding your question.

很抱歉误解了你的问题。

To check if a collection exists, use method collection_names on database:

要检查集合是否存在,请在数据库上使用方法collection_names:

>>> collection_name in database.collection_names()

To check if a collection is empty, use:

要检查集合是否为空,请使用:

>>> collection.count() == 0

both will return True or False in result.

两者都会在结果中返回True或False。

#3


5  

Have you tried this:

你试过这个:

db.collection.remove();