Mongo Update with Upsert


#1

Hi

it’s pretty easy and straightforward to do CREATE , RETRIEVE and DELETE of CRUD
but the UPDATE is what i am really stuck at with mongo using Dreamfactory live api

how can i achieve the following
get the record without using id and if it exists update (or append ) the data
if not exists then create it

sort of what mongo does with update with upsert
ref:http://docs.mongodb.org/manual/reference/method/db.collection.update/

thanks


#2

There’s some info at the end of this blog post about updating Mongo records with the API http://blog.dreamfactory.com/blog/bid/339945/NoSQL-No-Problem-MongoDB-Specifics.

There are a couple of options for implementing the “if it doesn’t exist then create the record, otherwise update the record” logic.

The client app can make a GET request and if the response is null then make a POST request to create a new record, else make a PUT (or PATCH) request on the returned record.

Alternatively, you could implement this same logic using a server-side script in DreamFactory (i.e., make a single GET request and let the server handle the POST vs PUT/PATCH logic instead of generating a second API call from the client).


#3

thanks ben for your prompt reply
the logic that you have mentioned worked perfectly
if in future you can put an example then it would definitely help other users

what i found is that i would like to share with you for mongodb upsert

getRecordsByFilter() just specify the
filter to be the criteria for e.g
{“0”: “bobthebuilder@norbiton@1”} all fields 0 whose content is bobthebuilder@norbiton@1

if it exists it will return the _id , else null

and for the update

updateRecord() - Update (patch) one record by identifier.

this works exactly in the same manner as i have wanted
just specify table name , id that you get previously
and in the body the fields that you need to change (if it won’t exist it will create it for you)

cheers


#4

Great, glad it worked :slight_smile: Thanks for sharing your upsert example.

Cheers,
Ben


#5

hi ben just a quickie

this works perfectly
window.df.apis.mymongo.getRecordsByFilter({“table_name”:“mytable”},function(response){
});

but how to pass filter as param in the api
getRecordsByFilter

i think of it as follows but definitely missing something out on this

window.df.apis.mymongo.getRecordsByFilter({“table_name”:“mytable”,“filter”:{“0”: “bobthebuilder”}},function(response){
});

cheers


#6

Should be “filter”:“field=value”

If your field name is ‘0’ and your field value is ‘bobthebuilder’ it would be

window.df.apis.mymongo.getRecordsByFilter({"table_name":"mytable","filter":"0=bobthebuilder"},function(response){
});

#7

thanks that helps alot
cheers


#8

hi todd just another quick one , it’s fantastically convenient to use Dreamfactory filter for non sql , as i am a noob when it comes to db esp mongo

the one thing that i am thinking about is
is it possible to pass a mongo query as a param to the live api

for e.g

db.inventory.find( { type: ‘food’, price: { $lt: 9.95 } } )
can this parameter { type: ‘food’, price: { $lt: 9.95 } } be passed as filter

i know above can be very easily done with DF live api
but what about subdocument for e,g
as on http://docs.mongodb.org/manual/reference/operator/projection/elemMatch/
db.schools.find( { zipcode: “63109” },
{ students: { $elemMatch: { school: 102 } } } )
thanks once again


#9

Here is a quick example of a mongo filter:

color is the column

white and blue are filters within the column

{
  "filter": {"color": {"$in": ["white","blue"]}}
}

But, let me take a crack at the drilling deeper. I will probably soon be corrected :smile:

   {
      "filter": {"students": {"school": {"$elemMatch": "102"}}}
    }

This may get you going in the right direction or maybe not… waiting to hear back from engineering until then, see how this works. You may need to add another layer for the zip. Most likely you will need to tweak this code a bit - let me know.

One last thing, the order in which these filter params are listed need to be in the hierarchy that matches your DB…

Thanks,

  • Mark

#10

Hello…

I have also great in using Mongodb
and I like all functionality. Here I would like to mention a link that Will
help you to do insert , Update and delete in a single article. All the things
is discussed properly. Feel free to visit: Click

.