Answer the question
In order to leave comments, you need to log in
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)
}
...
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
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
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question