1

I have a ONE TO MANY schema like this:

SHOP SCHEMA

const Shop = {

  name: "Shop",

  properties: {

      _id: "objectId",
      products:"Products[]"
 }
}

PRODUCTS SCHEMA

const Products = {

  name: "Products",

  properties: {

      _id: "objectId",
      name : "string",
    }
}

A shop has many products and as it can be seen 'pictorially' below

_id:'60f73ca7c1a70278596cc7d0',
products:[
      {_id:1, name:'product1'},
      {_id:2, name: 'product2'},
      {_id:3, name: 'product3'}
]

Now, say I want to delete product2, How do I do it with mongodb realm?

What I have tried so far

const obj = realm.objects('Shop').filtered("_id == $0 AND products._id == $1", ObjectId('60f73ca7c1a70278596cc7d0'), ObjectId('2'))
realm.write(() => {
   realm.delete(obj)
})

But this doesn't delete the item in the products array.

How can I achieve deleting a specific element in products array in this One to Many relationshiop using realm?

Amani
  • 227
  • 1
  • 10
  • For clarity; do you want to remove the product from the array or totally remove the product? e.g. removing it from the array does not delete the actual object. Also, your filter is going to return a Shop object, not a product object `obj = realm.objects('Shop')`. Is that the intention? If not, and you want to delete the product, you need to either filter for the Product object or, once you have the shop object (the results) then filter for the product you want to remove. – Jay Jul 21 '21 at 17:59
  • @Jay (i) I want to remove the product TOTALY(Deleting it). (ii) I want the filter to return a Product Object so that I can delete the desired product. (iii) Can you show me the Code on how to filter the product object so that I can remove the specific product? – Amani Jul 22 '21 at 12:08
  • That's actually covered in the documentation. First query for that object [Filter For Object](https://docs.mongodb.com/realm/sdk/react-native/examples/read-and-write-data/#filter-queries) and then once you have it, delete it [Delete an Object](https://docs.mongodb.com/realm/sdk/react-native/examples/read-and-write-data/#delete-an-object). If you want to delete product1, you can either query for "product1" or use it's ID if you know it. – Jay Jul 22 '21 at 17:03
  • @Jay I've already read those docs you are pointing here but I still COULD'T SOLVE my problem. Can you show me the WORKING SAMPLE CODE on how to solve my problem that I've explained in my question? – Amani Jul 23 '21 at 02:18

1 Answers1

2

The code in the question is very close to being correct, you just need to filter for the product you want to delete instead of the shop. It's not clear if you know the product _id or name but you can filter by either one.

Here's the code to filter for products with an _id of 1 and then delete it (which will also remove it from any lists that contain a reference to it.

const prod = realm.objects('Products').filtered("_id == 1");
realm.write(() => {
   realm.delete(prod);
   prod == null;
})

The above is taken from the documentation Filter Query and Delete An Object

Keep in mind this will delete all products with id = 1 so as long as _id's are unique it's fine.

Jay
  • 34,438
  • 18
  • 52
  • 81
  • Works perfectly. I was wrong for querying 'the mongodb way' by accesing the child from the parent. Querring the Products object direcly worked. Thanks – Amani Jul 23 '21 at 16:28