This is a migrated thread and some comments may be shown as answers.

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

3 Answers 196 Views
JavaScript SDK
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jonathan
Top achievements
Rank 1
Jonathan asked on 27 Aug 2016, 03:42 PM

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!

3 Answers, 1 is accepted

Sort by
0
Anton Dobrev
Telerik team
answered on 29 Aug 2016, 01:10 PM
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.
 
0
Jonathan
Top achievements
Rank 1
answered on 29 Aug 2016, 02:12 PM

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!

0
Pavel
Telerik team
answered on 01 Sep 2016, 09:58 AM
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.
 
Tags
JavaScript SDK
Asked by
Jonathan
Top achievements
Rank 1
Answers by
Anton Dobrev
Telerik team
Jonathan
Top achievements
Rank 1
Pavel
Telerik team
Share this question
or