push object to array or update existing object depending on object property value

4 posts, 0 answers
  1. Jonathan
    Jonathan avatar
    4 posts
    Member since:
    Jul 2016

    Posted 27 Aug Link to this post

    Is it possible to perform a push of an object to an array or update an existing object if it exists in the array depending on the value of an object property in the array in one command through the Javascript SDK? For example, if I have a field of type array as follows:

    contacts: [
      { 'name' : 'john', 'available' : true },
      { 'name' : 'doe''available' : false }
    ]

    Now if I take a new object where name:'john' (regardless of the property 'available') then I want to update the existing object for 'john'. However, if I have an object which name:'frank' then I want to add a new object to the array.

    I know I can use the operator $addToSet, but AFAIK that will insert another object into the array if for example name:'john' but available:'false'. I also saw the $elemMatch operator which works on queries. I guess I am looking for a combination of these two.

    Any advice?

    Thanks!

  2. Anton Dobrev
    Admin
    Anton Dobrev avatar
    539 posts

    Posted 29 Aug Link to this post

    Hello Jonathan,

    You may consider using the $ array operator.

    For example, with the following update expression:

    {
        "$set": {
            "contacts.$.available": true
        }
    }

    For a filter you can use the shorthand syntax, for example, all users that have the name john in their contacts:
    { "Contacts.name" : "john"}

    Let me know if this works for you.

    Regards,
    Anton Dobrev
    Telerik by Progress
     
    Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
     
  3. Jonathan
    Jonathan avatar
    4 posts
    Member since:
    Jul 2016

    Posted 29 Aug in reply to Anton Dobrev Link to this post

    Hi Anton,

    Thanks for answering all my questions. This is not entirely what I need, but it is possible to use upsert as is possible in Mongo (see https://docs.mongodb.com/manual/reference/method/db.collection.update/). It will allow me to do what I want by restructuring the schema a bit.

    Thanks!

  4. Pavel
    Admin
    Pavel avatar
    6 posts

    Posted 01 Sep Link to this post

    Hi Jonathan,

    Unfortunately upsert is not available as an option for the query. I believe that in MongoDB you cannot do an upsert based on a value of an object field in a embedded array.

    That being said even in pure MongoDB query terms your schema may need to be adjusted or use additional application logic in order to achieve what you want . This leads me to my next point, as I believe what you want can be achieved by Cloud Code. In order for me to help you with that I would need to know more details of how you edit the data through your application.

    A simple approach will be to subscribe to the beforeUpdate event of your Content Type as explained here and make a select based on your filter to check if the the contacts field contains the value you want so you update or it needs to be inserted. Keep in mind this is just a rough example of how you can do it and I will need more details for an exact approach. This however, may not be the fastest and most performant approach.

    Also note that the $addToSet operator appears to be the closest to what you would like to achieve.

    Do not hesitate to contact us again if you need further assistance.

    Regards,
    Pavel
    Telerik by Progress
     
    Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
     
Back to Top