Help with performance - DocumentDB

Hey guys, can you guys help me with an issue using mongo?

I have a database, that has 4 fields, and I register more than 100k new registers in a day, and I was having a lot of performance issues checking if the register it’s already created(to prevent duplicate registers).

For example:

I did the

const checkIfExist = db.collection.find({field1: ''x"},{field2: ''x"},{field3: ''x"},{field4: ''x"});

if(!checkIfExist ) {
    db.collection.create({field1: ''x"},{field2: ''x"},{field3: ''x"},{field4: ''x"})
}

Also, I tried to use updateOne but I also get a performance issue

PS: I am using AWS DocumentDB r52x.large, so isn’t problem of the cloud machine.

If I create some index unique, that check if these 4 fields it’s unique, it’s good for performance?

Have any other solution to improve the performance to create registers, without duplicated registers?

Welcome to the MongoDB Community @Matheus_Lopes !

Amazon DocumentDB is an independent emulation of a subset of features for the associated MongoDB server version they claim compatibility with.

The server implementations do not have any code in common, so behaviour like indexing may differ.

If you are trying to understand performance issues for DocumentDB, I recommend asking on Stack Overflow or an AWS product community: Newest ‘aws-documentdb’ Questions - Stack Overflow.

If your use case requires unique indexes, the main consideration is correctness rather than performance.

Unnecessary indexes will be unhelpful for performance as they take up RAM and add a bit of write I/O. Useful indexes will support common queries.

The general approach you are taking with two separate commands (find followed an insert) is subject to race conditions. The recommended pattern would be to Insert or Update in a Single Operation using an upsert.

Regards,
Stennie

2 Likes