Add multiple only unique documents


My goal is to create function which add multiple, but only unique documents. I have a function which adds multiple documents, but it stops with error when in array is even one not unique document.

I suppose that I can make check IF document is unique in function and then command to not create this document, but I feel like this solution duplicates index’s with unique terms behavior.

My function:

      q.Foreach(flats, q.Lambda('flat', q.Create(q.Collection('flats'), { data: q.Var('flat') })))

Is there any way of doing this?

Hi trzcinskiD!

Your supposition is correct: you would have to test for the existence of each new document prior to trying to create it.

To do this, you would need to extract the values from the flat object that the covering unique index uses in its terms definitions, call Match, and if there is a result, avoid creating the document.

Thank you, @ewan, I have done it that way.

I leave code here, if someone will need a similar function.

const newFlats = await client.query(
        q.IsNonEmpty(q.Paginate(q.Match(q.Index('all_flats_urls'), q.Select(['url'], q.Var('flat'))))),
        q.Create(q.Collection('flats'), { data: q.Var('flat') })

In the function, I’m creating many flat in ‘flats’ collection preserving unique value of url, which is value of flat object:

const flat = {
    url: "",
1 Like