Atlas Device SDK는 더 이상 사용되지 않습니다. 자세한 내용은 지원 중단 페이지 를 참조하세요.
객체 업데이트
다른 JavaScript 객체를 업데이트하는 것과 같은 방식으로 쓰기 트랜잭션(write transaction) 내에서 Realm 객체의 속성을 추가, 수정, 삭제할 수 있습니다.
// Open a transaction. realm.write(() => { // Get a dog to update. const dog = realm.objects("Dog")[0]; // Update some properties on the instance. // These changes are saved to the realm. dog.name = "Maximilian"; dog.age += 1; });
팁
관련 객체 및 내장된 객체 업데이트
To update a property of an embedded object or a related object, modify the property with dot-notation or bracket-notation as if it were in a regular, nested object.
객체 업서트
객체를 업서트하려면 업데이트 모드를 modified
로 설정하고 Realm.create() 를 호출합니다. 이 작업은 지정된 기본 키를 사용하여 새 객체를 삽입하거나 해당 기본 키가 이미 있는 기존 객체를 업데이트합니다.
realm.write(() => { // Add a new person to the realm. Since nobody with ID 1234 // has been added yet, this adds the instance to the realm. person = realm.create( "Person", { _id: 1234, name: "Joe", age: 40 }, "modified" ); // If an object exists, setting the third parameter (`updateMode`) to // "modified" only updates properties that have changed, resulting in // faster operations. person = realm.create( "Person", { _id: 1234, name: "Joseph", age: 40 }, "modified" ); });
컬렉션 대량 업데이트
To apply an update to a collection of objects, iterate through the collection (e.g. with for...of). In the loop, update each object individually:
realm.write(() => { // Create someone to take care of some dogs. const person = realm.create("Person", { name: "Ali" }); // Find dogs younger than 2. const puppies = realm.objects("Dog").filtered("age < 2"); // Loop through to update. for (const puppy of puppies) { // Give all puppies to Ali. puppy.owner = person; } });