Syntax

What is CRUD?

  • MongoDB Database에서 데이터를 Manipulating (조작) 하는 방법은 크게 CRUD로 표현할 수 있습니다.

    • Create: 새로운 Document 를 생성하는 일

    • Read: 이미 존재하는 Document 를 찾고 읽는 일

    • Update: 이미 존재하는 Document 의 정보를 업데이트 하는 일

    • Delete: 이미 존재하는 Document 를 제거하는 일

  • CRUD는 다양한 곳에서 사용된다.

    • RDBMS, NOSQL, WEB RESORUCE 긁어 올 때 (PUT, GET, POST, DELETE 등) 등..

Insert

  • Insert syntax

    • insertOne: insert a one single document

    • insertMany: insert multiple documents

collection.insertOne(<document>)
collection.insertMany([<docuemtn1>, <document2>, ...])
collection.insert(<document>)

Update

  • The syntax is identical to update documents between Mongo shell and Pymongo

    • Note that Pymongo uses a dictionary as a document in MongoDB.

    • Warn: Update operators must be written with ' or " , e.g., '$pull‘

  • $set, $unset, $inc, $push, $each, $addToSet, $pop, $pull, $slice, etc...

    • See a below link that is a link for the update operators

Operator

Read

  • An empty query document (i.e. {}) matches everything in the collection.

  • Add key/value pairs to restrict the search.

  • Add more key/value pairs for multiple conditions.

  • Pass a second argument to specify which keys to return/exclude.

Delete

  • syntax

    • deleteOne: remove a document.

    • deleteMany: remove documents.

Cursor

  • syntax

  • MongoDB always performs in the order of sort first, skip second, and limit

    • Specifies the order in which the query returns matching document.

    • Note that below two queries return the same answer.

  • Count documents in the store collection.

Explain()

Explain은 대부분 데이터베이스 시스템에서 지원을 해주는 기능이다. 사용자의 질의문 (query)을 데이터베이스의 optimizer가 query plan을 통해서 실행하는 것을 보여주는 것이다.

  • Provides information on the query plan for find() method.

  • In PyMongo, explain() returns a dictionary. Use a key, ‘executionStats’.

  • Example

explain() in MongoShell

explain() in PyMongo

Last updated

Was this helpful?