Good day. There is a collection of documents of the following form:

[ {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 a second resource, I’ll get a User.guid duplication error.

Paste {Resource: {guid: 4, prop: 3}} does not work :-(

The index looks like this:

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

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

    1 answer 1

    It was necessary so:

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