简体   繁体   中英

Find/Search performance in mongoDB

I have worked a lot with sql like databases but when it comes to nosql dbs my knowledge is limited. Planning the design of my application I asked myself if there are differences when you want to find an entry in the database and the parameters used for identification.

For example: To avoid duplicates I want to check the database if the entry exists before using the insertOne command. Are there any differences whether I use the _id field or an attribute I defined myself in the query when it comes to efficiency and speed? I think that the collection will not hold more than 10.000 items.

Because I'm able to set the _id field when inserting a dataset manually this could impact the overall performance.

Like with SQL databases, you can add indexes on arbitrary fields in MongoDB. There should not be a noticable difference between reads based on the _id index or on your own.

To avoid duplicates I want to check the database if the entry exists before using the insertOne command.

You should have a look at the upsert parameter which handles exactly this usecase. It either updates or inserts a document (hence the name, up-sert). Refer the MongoDB Node.js driver documentation for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM