A
A
Alexey Dugnist2016-03-25 15:11:13
JavaScript
Alexey Dugnist, 2016-03-25 15:11:13

How to correctly formulate a query in mongo?

There is a collection in mongo, like this:

{
  id: '123',
  name: 'example',
  group: [
    {
      id: '456',
      name: 'example2'
    },
    {
      id: '789',
      name: 'example3'
    }
  ]
}

You need to get a user from a group by id.
I make the following query in mongo: { 'group' : { $in : [ { id: user.id } ] } }
Doesn't work, because it requires only the id itself, but I have a name - how to make the selection correctly... ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Super User, 2016-03-25
@Peekab0o

db.mycollection.find( { "group.id": 456 } );

R
Rakhim Davletkaliev, 2016-03-25
@freetonik

Why not use a simple find?
db.users.find( { id: 123 } )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question