Good afternoon. There is a collection of documents of the following type:

{User: {guid: 1, prop: 1}},
{User: {guid: 2, prop: 2}},
{Resource: {guid: 3, prop: 3}},

There is a task to index User.guid as unique. But when creating the second resource, I get a duplicate User.guid error.

Paste {Resource: {guid: 4, prop: 3}} won't work :-(

The index looks like this:

... ensure_index ({'User.guid': 1}, {unique: true});

I haven’t found the answer to the question on the Internet yet :-(

  • Answer # 1

    It was necessary like this:

    ensure_index ({'User.guid': 1}, {unique: true, sparse: true});