model.destroy/remove by other than id

36 views
Skip to first unread message

António Rodrigues

unread,
May 25, 2016, 11:25:55 AM5/25/16
to LoopbackJS
Hello,

I need the following (@Node syntax):

1) Import several rows from Excel (done);
2) Delete *all* records from persistedModel where "externalId" equals a value extracted from the imported data;

The Excel can contain several hundred records so I don't want to:
let DeleteID = model.find({externalId: myExcelValue} ...) for every row, and if found call another model.destroyById({id: DeleteID})

Is there a quick or single method i can use to accomplish this task, or do I missed something?

Thank you!

AR

Marek Jaskot

unread,
May 25, 2016, 4:13:59 PM5/25/16
to LoopbackJS
Hi,
You can try use inq but first you must collect all value in array... for example:

let toDelete= [myExcelValue1,myExcelValue2,...]

model.destroyAll( { where: { externalID: { inq: toDelete } } }

more info here:

António Rodrigues

unread,
May 25, 2016, 4:19:49 PM5/25/16
to LoopbackJS
Marek, thanks for the tip!
Will try it out.

António Rodrigues

unread,
May 31, 2016, 4:45:22 AM5/31/16
to loopb...@googlegroups.com
EDIT:
Working now ;)
Removed the {where:} clause, and it worked -- I think I forgot to save after removing it in the first place...

Thank you.

Ok, still not working with MongoDB. 

I have tried with where clause, but i think is not applicable to .destroyAll() -- maybe it's only valid for .find()?
Removed the where clause, still doesn't delete any records.

Bug?
 

On Wednesday, May 25, 2016 at 9:13:59 PM UTC+1, Marek Jaskot wrote:

António Rodrigues

unread,
Jun 2, 2016, 6:31:04 AM6/2/16
to loopb...@googlegroups.com
Any toughs on using multiple parameters besides "inq"?

I'm trying the following, but no success so far.
Documentation is sparse about adding other filters to inq...

On a single operation, I want to be able to delete all myModel instances where:
projectid = 12345 *and* functionalId's in [66,77,88,999....] 

This does not work:

myModel.deleteAll({and: [
                          {projectId: projectId},
                          {functionalId: {inq: idsToDelete}}
                        ]}, function(err, results){


Thank you!
Reply all
Reply to author
Forward
0 new messages