How do I search different documents and subdocuments mongodb

0

I wanted to do a find on a login screen where he checked both the login field of the document and login in the subdocument. Can you do that?

{
 Login: admin,
Senha : 12345,
_id : 73h2b2k18bdjd88,
Nivel : 1
Usuarios [
    { 
       Login : andre,
      Senha : 17344,
     _id : 73b3hjd8did,
    Nivel : 2
   }
]
}
    
asked by anonymous 28.07.2017 / 11:34

1 answer

0

From what I saw in the example the _id does not have a link: If you have a link you can do a lookup in the collection Usuario instead of the project, I did on top of the result you put as an example

You can do this:

db.login.aggregate([
{
    $match: {
        'Login': 'admin',
        'Senha': '12345',
    }
},
{
    $project :{
        login: db.login.distinct('Usuarios.Login', { 'Usuarios.Login': 'andre' }),
        senha: db.login.distinct('Usuarios.Senha', { 'Usuarios.Login': 'andre' }),    
    }
},
{ $unwind: { 'path': '$login', 'preserveNullAndEmptyArrays': false } },
{ $unwind: { 'path': '$senha', 'preserveNullAndEmptyArrays': false } },
{
    $match: {
        'login': 'andre',
        'senha': '17344',
    }
}
])
    
03.10.2017 / 01:43