0

I have below object :

obj1 = [{ id = 1, name = "abc"}, {id=2, name="pqr"}, {id=3, name="xyz"}]

I need to delete an object with id=2 where id is Primary Key too. Below method using to delete the object

 const collection = RealmDB.realm
      .objects("StudentName")
      .filtered(`id= $0`, '65');
    RealmDB.realm.write(() => {
      RealmDB.realm.delete(collection);
    });

But it is not working with id object can anyone please suggest better way to do this ?

But still that object is there so may I know what is wrong here.

Swift
  • 829
  • 2
  • 12
  • 33
  • It looks like you have a main object with a list of objects; are you trying to remove an object from the main objects list or totally remove the object completely? – Jay Jun 22 '21 at 15:28
  • yes trying to remove object from main objects list. – Swift Jun 22 '21 at 17:13

2 Answers2

0
const id = 1;
realm.write(()=>{
   realm.delete(realm.objectForPrimaryKey('Baby',id));
}) 

Try this.

gwl002
  • 654
  • 4
  • 10
0

Hello resolved an issue by following query with no error

const collection = RealmDB.realm
          .objects('StudentName')
          .filtered('id= $0', `65`);
        RealmDB.realm.write(() => {
          RealmDB.realm.delete(collection);

Thank you

Swift
  • 829
  • 2
  • 12
  • 33
  • You don't need to filter for an object that has a primary key. It can be [directly read](https://docs.mongodb.com/realm/sdk/react-native/examples/read-and-write-data/#read-operations); like this `const student = realm.objectForPrimaryKey("StudentName", $0);` – Jay Jun 22 '21 at 17:23
  • what is $0 and what about if wanted to filter for 65 id – Swift Jun 23 '21 at 16:25
  • Hmmm `$0` is from your code -> `.filtered('id= $0', `65`);` and is typically a placeholder. Either way - that's the code you need, just use `65` if that's the primary key value of the object you want to update. – Jay Jun 23 '21 at 18:43