[MongoDB] 도큐먼트 삭제

참조

  • 몽고DB 완벽가이드 3판


1.1 도큐먼트 삭제

  • 데이터베이스에 있는 데이터를 삭제하는 방법에 대해서 알아 보겠습니다.
  • 이를 위해 CRUD API는 deleteOne과 deleteMany를 제공합니다.
  • 두 메서드 모두 필터 도큐먼트를 첫 번째 매개변수로 사용합니다.
  • 필터는 도큐먼트를 제거할 때 비교할 일련의 기준을 지정합니다.
  • _id 값이 4인 도튜먼트를 삭제하려면 아래와 같이 deleteOne 을 사용하면 됩니다.

삭제 전

// MongoDB Playground
// To disable this template go to Settings | MongoDB | Use Default Template For Playground.
// Make sure you are connected to enable completions and to be able to run a playground.
// Use Ctrl+Space inside a snippet or a string literal to trigger completions.

// Select the database to use.
use('jbh_MongoDB');

db.movies.drop();

db.movies.insertMany([
                      {"_id" : 0, "title" : "Top Gun"},
                      {"_id" : 1, "title" : "태극기 휘날리며"},
                      {"_id" : 2, "title" : "어벤져스"},
                      {"_id" : 3, "title" : "이터널 션샤인"},
                      {"_id" : 4, "title" : "쏘우"},
                      {"_id" : 5, "title" : "해운대"}
                    ]);

db.movies.find();
[
  {
    "_id": 0,
    "title": "Top Gun"
  },
  {
    "_id": 1,
    "title": "태극기 휘날리며"
  },
  {
    "_id": 2,
    "title": "어벤져스"
  },
  {
    "_id": 3,
    "title": "이터널 션샤인"
  },
  {
    "_id": 4,
    "title": "쏘우"
  },
  {
    "_id": 5,
    "title": "해운대"
  }
]


삭제 후

// MongoDB Playground
// To disable this template go to Settings | MongoDB | Use Default Template For Playground.
// Make sure you are connected to enable completions and to be able to run a playground.
// Use Ctrl+Space inside a snippet or a string literal to trigger completions.

// Select the database to use.
use('jbh_MongoDB');

db.movies.drop();

db.movies.insertMany([
                      {"_id" : 0, "title" : "Top Gun"},
                      {"_id" : 1, "title" : "태극기 휘날리며"},
                      {"_id" : 2, "title" : "어벤져스"},
                      {"_id" : 3, "title" : "이터널 션샤인"},
                      {"_id" : 4, "title" : "쏘우"},
                      {"_id" : 5, "title" : "해운대"}
                    ]);

db.movies.deleteOne({"_id" : 4});

db.movies.find();
[
  {
    "_id": 0,
    "title": "Top Gun"
  },
  {
    "_id": 1,
    "title": "태극기 휘날리며"
  },
  {
    "_id": 2,
    "title": "어벤져스"
  },
  {
    "_id": 3,
    "title": "이터널 션샤인"
  },
  {
    "_id": 5,
    "title": "해운대"
  }
]
  • _id 가 4인 쏘우 데이터가 삭제된 것을 확인할 수 있습니다.


deleteOne 특징

  • deleteOne 함수를 이용하여 컬렉션 내 여러 도큐먼트와 일치하는 필터도 지정할 수 있습니다.
  • 이때 deleteOne은 필터와 일치하는 첫 번째 도큐먼트를 삭제합니다.
  • 어떤 도큐먼트가 먼저 발견되는지는 도큐먼트가 삽입된 순서, 도큐먼트에 어떤 갱신이 이루어 졌는지에 따라 다를 순 있습니다.
// MongoDB Playground
// To disable this template go to Settings | MongoDB | Use Default Template For Playground.
// Make sure you are connected to enable completions and to be able to run a playground.
// Use Ctrl+Space inside a snippet or a string literal to trigger completions.

// Select the database to use.
use('jbh_MongoDB');

db.movies.drop();

db.movies.insertMany([
                      {"_id" : 0, "title" : "Top Gun"},
                      {"_id" : 1, "title" : "태극기 휘날리며"},
                      {"_id" : 2, "title" : "어벤져스"},
                      {"_id" : 3, "title" : "해운대"},
                      {"_id" : 4, "title" : "해운대"},
                      {"_id" : 5, "title" : "해운대"}
                    ]);

db.movies.deleteOne({"title" :"해운대"});

db.movies.find();
[
  {
    "_id": 0,
    "title": "Top Gun"
  },
  {
    "_id": 1,
    "title": "태극기 휘날리며"
  },
  {
    "_id": 2,
    "title": "어벤져스"
  },
  {
    "_id": 4,
    "title": "해운대"
  },
  {
    "_id": 5,
    "title": "해운대"
  }
]
  • 위와 같이 _id 3, 4, 5의 title은 모두 "해운대" 로 동일합니다.
  • deleteOne을 통해 title이 "해운대" 인 데이터를 삭제 해보면, 제일 먼저있는 _id 3번 데이터만 삭제되고, 4, 5번의 _id 는 삭제되지 않은 것을 확인할 수 있습니다.


deleteMany

  • deleteMany 함수를 통해 필터와 일치하는 모든 도큐먼트를 삭제할 수 있습니다.
// MongoDB Playground
// To disable this template go to Settings | MongoDB | Use Default Template For Playground.
// Make sure you are connected to enable completions and to be able to run a playground.
// Use Ctrl+Space inside a snippet or a string literal to trigger completions.

// Select the database to use.
use('jbh_MongoDB');

db.movies.drop();

db.movies.insertMany([
                      {"_id" : 0, "title" : "Top Gun"},
                      {"_id" : 1, "title" : "태극기 휘날리며"},
                      {"_id" : 2, "title" : "어벤져스"},
                      {"_id" : 3, "title" : "해운대"},
                      {"_id" : 4, "title" : "해운대"},
                      {"_id" : 5, "title" : "해운대"}
                    ]);

db.movies.deleteMany({"title" :"해운대"});

db.movies.find();
[
  {
    "_id": 0,
    "title": "Top Gun"
  },
  {
    "_id": 1,
    "title": "태극기 휘날리며"
  },
  {
    "_id": 2,
    "title": "어벤져스"
  }
]
  • 위와 같이 title이 "해운대" 인 도큐먼트는 모두 삭제된 것을 확인할 수 있습니다.


drop

  • deleteMany를 이용하여 전체 도큐먼트를 삭제할 수 있습니다.
// MongoDB Playground
// To disable this template go to Settings | MongoDB | Use Default Template For Playground.
// Make sure you are connected to enable completions and to be able to run a playground.
// Use Ctrl+Space inside a snippet or a string literal to trigger completions.

// Select the database to use.
use('jbh_MongoDB');

db.movies.drop();

db.movies.insertMany([
                      {"_id" : 0, "title" : "Top Gun"},
                      {"_id" : 1, "title" : "태극기 휘날리며"},
                      {"_id" : 2, "title" : "어벤져스"},
                      {"_id" : 3, "title" : "해운대"},
                      {"_id" : 4, "title" : "해운대"},
                      {"_id" : 5, "title" : "해운대"}
                    ]);

db.movies.deleteMany({});

db.movies.find();
[]
  • 하지만, 전체 도큐먼트를 삭제하는 경우에는 drop 함수를 이용하는 것이 더 빠릅니다.
// MongoDB Playground
// To disable this template go to Settings | MongoDB | Use Default Template For Playground.
// Make sure you are connected to enable completions and to be able to run a playground.
// Use Ctrl+Space inside a snippet or a string literal to trigger completions.

// Select the database to use.
use('jbh_MongoDB');

db.movies.insertMany([
                      {"_id" : 0, "title" : "Top Gun"},
                      {"_id" : 1, "title" : "태극기 휘날리며"},
                      {"_id" : 2, "title" : "어벤져스"},
                      {"_id" : 3, "title" : "해운대"},
                      {"_id" : 4, "title" : "해운대"},
                      {"_id" : 5, "title" : "해운대"}
                    ]);

db.movies.drop();
true
728x90

이 글을 공유하기

댓글

Designed by JB FACTORY