Christopher Herold
Posts: 0
Joined: Fri Jun 28, 2013 8:27 pm

Deleting Multiple DB Entries Using "Where"

I noticed on the DB tutorial page that an object_id is required to have a successful database deletion.

But what if I want to delete based on a specific aspect of the database? Like I want to delete all entries for a specific user_id.

For example:
DELETE * WHERE user_id = '5'

How do I go about that? Right now I have a WHERE parameter in the delete service, and then I feed in the user_id from local variables.

WHERE -- return '{"user_id":"'+value+'"}';

But that doesn't work.

Any ideas??

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Deleting Multiple DB Entries Using "Where"

Hello! It shouldn't work this way. You can retrieve list of all objects which satisfy condition (query request with necessary where parameter) and for each object you retrieved invoke Delete service. Another way is to use server code (the algorithm is the same, but it allows you to reduce data traffic).

Christopher Herold
Posts: 0
Joined: Fri Jun 28, 2013 8:27 pm

Deleting Multiple DB Entries Using "Where"

How do I do the delete each one after I get the list?

Having trouble wrapping my brain around this one.

Do you have any guidance for how to do this with server code?

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Deleting Multiple DB Entries Using "Where"

Hello!
1) "delete each one after I get the list" - on service success event when you retrieve all elements from list which satisfy where criteria, add JS where you go in a loop through entire array you get in response (data variable) and for each _id invoke Delete service passing object _id as request parameter. This link should help https://getsatisfaction.com/apperyio/...
2) "guidance for how to do this with server code" - please take a look here http://docs.appery.io/documentation/b...

Christopher Herold
Posts: 0
Joined: Fri Jun 28, 2013 8:27 pm

Deleting Multiple DB Entries Using "Where"

I'm sorry that I have to keep coming back to you on this. I really am working hard on it, but my background in MySQL is not helping much with the syntax for Mongo DB calls.

I am working on this in server code.

Can you help me troubleshoot the following? I am trying to collect my list of db rows in the result object. When I don't have any params.criteria I can get the entire database into the result object, but I can't figure out how to specify the "where" for this little bit of code that I found in your sever code tutorial.

I have searched the web for any similar code and can't find anything to give me any clue to how to do this.

Any thoughts or guidance??

code

result = {};

var params = {}; //Define parameters object
params.criteria = { //Query criteria:
//??How to structure search for where r_owner = + owner_id +??
//'r_owner': {"'+ owner_id +'"} //Doesn't work
'where={"r_owner": "' + owner_id + //Doesn't work
};

//Make the query and save it to the result object
result.query = Collection.query(db_id, collectionName, params);

//Output the results of the query
response.success(result);

/code

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Deleting Multiple DB Entries Using "Where"

Hello! Could you clarify what do you mean on pre+ owner_id +/preParameters are passed this way prevar params = {}; //Define parameters object
params.criteria = {
"r_owner": owner_id
};/prewhich means select all records where "r_owner" field is equal to owner_id

Christopher Herold
Posts: 0
Joined: Fri Jun 28, 2013 8:27 pm

Deleting Multiple DB Entries Using "Where"

Perfect. That works. Thanks again Maryna!

Ok, onto the final part!

How do I loop through my coderesult/code object and pull out the _id for each of the records? -- so that I can then delete that record.

I am currently working with:

code
for (item in result) {
if (!result.hasOwnProperty(item)) {
continue;
}
//Get _id of record and set to record_id
var record_id = ????
// delete record
Collection.deleteObject(db_id, collectionName, record_id);
}
/code

I am not sure how to set the _id for each retrieved record to the record_id.

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Deleting Multiple DB Entries Using "Where"

Hi Christopher,

Please try this code:prevar record_id = item;/preYou can check this value in console.

Christopher Herold
Posts: 0
Joined: Fri Jun 28, 2013 8:27 pm

Deleting Multiple DB Entries Using "Where"

How and where can I check this value in "console" in the ServerCode section?

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Deleting Multiple DB Entries Using "Where"

Hello!
1) "How and where can I check this value in "console" in the ServerCode section?" - do console.log for what you need, after testing check results on Trace tab
2) Instead for (item in result) try prevar i, len, record_id;
for(i = 0, len = result&#46;length; i < len; i++) {
record_id = result['_id'];
Collection&#46;deleteObject(db_id, collectionName, record_id);
}/pre

Return to “Issues”