The best way to consult a large amount of data with MongoDB

2

I have a 1.6GB collection that basically have 2 data: nombre and documento .

The document has this structure:

{
  "_id":  ObjectId("5a6ba42850c4631898754a06"),
  "nombre": "Jorge",
  "documento": "4918273627"
}

I look for them by documento and return the nombre .

My question is if there is a way to optimize it so that the results appear faster, because the query takes a long time and I understand that this type of database is much faster than relational.

The query I do with Mongoose in NodeJS and it is as follows:

    MongoDB.collection('datos').findOne({ 'documento': { $regex: new RegExp("^" + documento.toLowerCase(), 'i') } }, function (error, ok) {

Thanks for your contributions.

    
asked by Francisco Tamayo 07.02.2018 в 23:58
source

1 answer

2

If you're only looking for exact matches , you should use the $eq :

'documento': { 
    $eq: documento
}
  • It does not make sense to use a regex when you're looking for an exact match.


But if you are looking for "all the documents that start with ...", as in the code of your question, seeing that the documents are numbers, it does not make sense to use the i modifier to ignore uppercase and lowercase. The problem of ignoring uppercase / lowercase in MongoDB is that it deactivates the indexes in the search, making it soooooo much less efficient.

To find the documents that start with :

'documento': { 
    $regex: '^' + documento
}


Of course, make sure you have created an index for documento with db .collection.createIndex () . That will ensure that the search is quick.

    
answered by 08.02.2018 / 02:40
source