Answer the question
In order to leave comments, you need to log in
How to find in mongodb by object key name?
I have a sessions object where each time a new property-object named session_id is added.
I need to find a user who has a property named session_id in the sessions object.
How to do it ?
Tried through where, it is impossible.
Is it possible to enter a string in place of session_id, but how to pass a variable, it gives an error that session_id is undefined
const {username, session_id} = user
console.log('/load',session_id)
let result = null;
User.findOne({ $where: function() {
if (this.sessions["<session_id>"]) {
return true
}
} }, {username: 1, email: 1})
Answer the question
In order to leave comments, you need to log in
Плохое решение, так сессию не заиндексировать - а значит монга будет делать full scan коллекции, что приведет к тормозам при росте коллекции.
Как компромис - можете продублировать сессю в массив и сделать на него индекс.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question