개요
In this guide, you can learn about the Mongoid::Document module in Mongoid. The Document module is a Ruby implementation of a MongoDB document, which stores data in field-and-value pairs. To learn more about the terminology, structure, and limitations of MongoDB documents, see Documents in the Server manual.
MongoDB 에 유지하려는 모든 클래스에 Mongoid::Document 모듈을 포함해야 합니다. 모델 클래스에 Document 모듈을 포함하면 모델 클래스의 인스턴스에서 해당 메서드를 사용할 수 있습니다.
다음 코드는 샘플 Person 모델 클래스에 Document 모듈을 포함하는 방법을 보여줍니다.
class Person include Mongoid::Document field :name, type: String end
모듈에 대한 자세한 Document 내용은 API 설명서에서 확인할 수 있습니다.
문서 작업
모델의 인스턴스를 컬렉션 에 직접 저장 하거나 Document 모듈을 사용하는 다른 클래스에 포함할 수 있습니다. Document 인스턴스 MongoDB 에 저장하면 Ruby 해시 또는 JSON 객체 와 유사한 BSON 객체 로 변환됩니다.
다음 코드는 이전 섹션에서 정의된 Person 모델의 인스턴스 만듭니다.
Person.create(name: 'Meena Kumar')
문서 MongoDB 에 다음과 같이 표시됩니다.
{ "_id": { "$oid": "673b6dce61700598c24a72b0" }, "name": "Meena Kumar" }
참고
_id 필드
모델의 인스턴스 데이터베이스 에 유지하면 MongoDB 모델에서 이 필드 명시적으로 정의하지 않더라도 고유 값을 가진 _id 필드 자동으로 추가합니다.
이 필드 에 대해 자세히 학습 서버 매뉴얼의 ObjectId 참조를 참조하세요.
추가 정보
Mongoid 모델에서 사용할 수 있는 필드 유형에 대해 자세히 학습하려면 BSON 필드 유형 사용 가이드를 참조하세요.
To learn how to access and change your MongoDB data, see CRUD Operations.