2

I want to remove all documents from a database except 1, the configuration document. I have the following ssjs code:

var dc:NotesDocumentCollection = database.getAllDocuments();
var vw:NotesView = database.getView("configuration");
var doc:NotesDocument = vw.getFirstDocument();
if (dc.getCount() > 0){
    if (doc != null){
        dc.deleteDocument(doc);
    }
    dc.removeAll(false);
}

however when I run the script I get an error on command dc.deleteDocument(doc);

What am I doing incorrect?

Lothar Mueller
  • 2,528
  • 1
  • 16
  • 29
Malin
  • 697
  • 5
  • 21

2 Answers2

4

Use

     dc.subtract(doc);

instead of deleteDocument(). This is the recommended way to remove a document from a collection.

Knut Herrmann
  • 30,880
  • 4
  • 31
  • 67
  • thank you that worked! why is the deleteDocument method not working? – Malin Mar 08 '16 at 10:09
  • 1
    documentation for deleteDocument says: "the specified document must have originated in this collection...". For example, this method can be used to remove docs from a collection while looping through it – Lothar Mueller Mar 08 '16 at 12:31
  • 2
    I.e., "must have originated in this collection" refers to the object reference in memory. In your case, doc originated in the NotesView vw, It doesn't contain a pointer back to its position in the NotesDocmentCollection dc, so dc.deleteDocument(doc) doesn't know where in the collection to remove it from. The dc.Subtract method knows how to work with multiple sources. It builds an index of noteids from each source, and rebuilds the collection with the appropriate noteids removed. It's less efficient, but far more useful. – Richard Schwartz Mar 08 '16 at 15:02
  • AFAIK, if your configuration document were a profile document, you wouldn't have to bother about it: profile documents do not appear in database.getAllDocuments(). – D.Bugger Mar 10 '16 at 08:45
  • @D.Bugger: profile documents can be a pain in production environment as they are pretty buggy. I don't recommend to use profile documents from my experience. – Knut Herrmann Mar 10 '16 at 08:50
0

They can be a pain indeed, but they are not buggy, IMHO. One just has to know that they behave somewhat differently, especially when using replication, for replication conflicts never merge. That shouldn't stop you from using them, the advantages largely outweigh the inconveniences, in my experiences...

D.Bugger
  • 2,300
  • 15
  • 19