Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ /
Atlas Device SDK
/ /

CRUD - 업데이트 - Node.js 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;
}
});

참고

역관계

Dog.ownerPerson.dogs역관계 덕분에 Realm은 Ali를 강아지의 주인으로 설정할 때마다 Ali의 개 목록을 자동으로 업데이트합니다.

돌아가기

읽기

이 페이지의 내용