P
P
partyzanx2020-03-02 12:57:12
MongoDB
partyzanx, 2020-03-02 12:57:12

How to speed up the process of extracting from the database?

I am making a dictionary. The user makes a request, say, 200 hieroglyphs.

The request goes like this

app.get('/search', async (req, res) => {  
searchingBy = '美术设计师美术设计师美术设计师美术设计师美术设计师美术设计师美术设计师美术设计师美术设计师美术设计师美术设计师美术设计师美术设计师美术设计师美术设计师美术设计师美术设计师美术设计师美术设计师美术设计师'
 searchingBy = searchingBy.split('');
...
    let arr = []; 
    for (let i of searchingBy) {
        let e = await Word.find({characters: i});
        arr = arr.concat(e)
    }
...


It turns out that the site takes a long time to load, because there are many iterations.
How to make iterations happen in parallel to each other?
Please teach. I'm new to this business.
As I understand it, it is necessary somehow on promises or something ...

The whole file is here
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const compression = require('compression'); 
const cookieParser = require('cookie-parser');
const mongoose = require('mongoose'); 
const helmet = require('helmet'); 
const path = require('path'); 
const db = mongoose.connection;
const keys = require('./keys/index');
const variables = require('./middlewares/variables');
const Word = require('./models/word'); 
  
 

app.set("view engine", "pug");
app.use(cookieParser())
app.use(bodyParser.urlencoded({ extended: true }));
app.use('/public', express.static(path.join(__dirname, 'public')));
app.use(helmet());
app.use(compression());

app.use(variables);
 
app.get('/', (req, res) => {   
    let lang = req.cookies.lang;
    res.render('index', {
        pageTitle: 'mainPage',
        lang
    });
}); 

 

app.get('/search', async (req, res) => {  
    let lang = req.cookies.lang;
    let {query: {q: searchingBy }} = req; 

 
    searchingBy = searchingBy.split('');
 
    let arr = []; 
    for (let i of searchingBy) {
        let e = await Word.find({characters: i});
        arr = arr.concat(e)
    }
    searchingBy = arr; 
    res.render('search', { 
        pageTitle: 'Search',
        searchingBy,
        lang
    });
}); 


const PORT = process.env.PORT || 1000;

async function start() {

    try {
    await mongoose.connect(keys.MONGODB_URI, {useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true, useFindAndModify: false});  
     
    app.listen(PORT, () => {
        console.log(`Listen to ${PORT}`)
    })
    } catch(e) {
        console.log(e)
    }

} 

start();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
hzzzzl, 2020-03-02
@partyzanx

await Word.find({characters: i});
that is, is Word.characters there a single character, or an array?
look at the $in mongodb operator
https://docs.mongodb.com/manual/reference/operator...

await Word.find({ 
  characters: { 
    $in: [...searchingBy]   // когда characters есть в массиве searchingBy
  } 
});

A
Alex, 2020-03-02
@Kozack

// Паралельный поиск, но в одном потоке
let arr = await Promise.all( searchingBy.split('').map(i => Word.find({characters: i}) ) )

But, it's best to remake Word.findit so that it searches for several characters at once

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question