S
S
squadbrodyaga2020-12-17 15:07:35
Node.js
squadbrodyaga, 2020-12-17 15:07:35

How to find an object in an array in MongoDB?

Hello, I need to get a certain object from the array by the Y and X keys, now I'm doing it somehow crookedly, except for the fact that this code does not work at all:

async function findInArray(Y, X) {
   let game = await Game.findById(какой-то ИД)
   // allClick – это массив из объектов, по ключам Y и X ищу нужный мне объект 
   let player = game.allClick.find(k => k.Y == Y && k.X == X)
   return player
}

Is it possible to somehow enter the object I need immediately into the game variable, and not create a separate player variable for it?

The database itself looks like this:
_id: 1
...
...
allClick: [
  0: {Y: 2, X: 5, player: 0xD34F}
  1: {Y: 7, X: 3, player: squadbrodyaga}
  2: ...
]
__v: 0

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuriy Vorobyov, 2020-12-17
@squadbrodyaga

Based on your comment one might conclude that you should call the function as await findInArray(Y, X)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question