How do I search different mongodb documents and subdocuments

Asked

Viewed 157 times

0

I wanted to make a find on a screen of login where he checked so much in the field login of the document and login subdocumentation. You can do this?

{
 Login: admin,
Senha : 12345,
_id : 73h2b2k18bdjd88,
Nivel : 1
Usuarios [
    { 
       Login : andre,
      Senha : 17344,
     _id : 73b3hjd8did,
    Nivel : 2
   }
]
}
  • If you can post subdocuments, it’s better to help.

1 answer

0

From what I saw in the example the _id does not have a link: If you have a bond you can make one lookup in Collection Usuario in place in the project, I did on top of the result you put as an example

You can do it like 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',
    }
}
])

Browser other questions tagged

You are not signed in. Login or sign up in order to post.