首页 > 解决方案 > mongodb中的删除方法

问题描述

我正在尝试在 MongodB 中创建 CRUD 操作,但无法在其中创建删除操作。下面的代码用于动物收容所,我首先插入数据,然后使用 find 关键字读取数据,我只是对删除操作感到困惑,因为我想删除 - 说所有行。那么如何使用

删除许多()

from pymongo import MongoClient

class AnimalShelter(object):

  """ CRUD operations for Animal collection in MongoDB """



  def __init__(self, username, password):

    # Initializing the MongoClient. This helps to 

    # access the MongoDB databases and collections. 

    if username and password:

      self.client = MongoClient('mongodb://%s:%s@localhost:27017' % (username, password))

    else:

      self.client = MongoClient('mongodb://localhost:27017')

    self.database = self.client['AAC']



# Complete this create method to implement the C in CRUD.

  def create(self, data):

    if data is not None:

      insert = self.database.animals.insert(data) # data should be dictionary 

      if insert!=0:

        return True

      else:

        return False      

    else:

      raise Exception("Nothing to save, because data parameter is empty")





  # Create method to implement the R in CRUD.

  def read(self,criteria=None):



    # criteria is not None then this find will return all rows which matches the criteria

    if criteria:

     # {'_id':False} just omits the unique ID of each row     

       

      data = self.database.animals.find(criteria,{"_id":False})

    else:

    #if there is no search criteria then all the rows will be return  

      data = self.database.animals.find( {} , {"_id":False})



    return data

   

  # The delete method in CRUD

(missing)

标签: pythonmongodb

解决方案


推荐阅读