Why does my query return all documents instead of matching?

All my life had beeb Oracle Development, just right now I’m starting witn mongoDB, and I need help with this query 'cause don’t work as I wish, it retrive all rows and not a specific row.

> db.layouts.find({}, {_id: 1})
{ "_id" : "adresses" }
{ "_id" : "clients" }

> db.layouts.find({_id: "clients"}, {"info.id": 1, "info.married": 1, _id: 0})
{ "info" : [ { "id" : "luis.alvarez", "married" : 1 }, { "id" : "jose.perez", "married" : 0 }, { "id" : "daniel.contreras", "married" : 0 }, { "id" : "javier.pirela", "married" : 0 } ] }

> db.layouts.find({_id: "clients", "info.married": "1"}, {"info.id": 1, "info.married": 1, _id: 0})

> db.layouts.find({_id: "clients", "info.married": 1}, {"info.id": 1, "info.married": 1, _id: 0})
{ "info" : [ { "id" : "luis.alvarez", "married" : 1 }, { "id" : "jose.perez", "married" : 0 }, { "id" : "daniel.contreras", "married" : 0 }, { "id" : "javier.pirela", "married" : 0 } ] }

> db.layouts.find({_id: "clients", "info.id": "luis.alvarez"}, {"info.id": 1, "info.married": 1, _id: 0})

> db.layouts.find({_id: "clients", "info.married": 1}, {"info.id": 1, "info.married": 1, _id: 0})
{ "info" : [ { "id" : "luis.alvarez", "married" : 1 }, { "id" : "jose.perez", "married" : 0 }, { "id" : "daniel.contreras", "married" : 0 }, { "id" : "javier.pirela", "married" : 0 } ] }

As you can see, for the document clients (_id=clients) and married marital status (married=1)retrieves all clients regardless of status, why is this?

Thanks in advance by your help !!!

Welcome to the community, @Carlos_Aldana!

To understand this, you need to start thinking in MongoDB terms.

You see, you have only 2 documents (rows) in your ‘layouts’ collection (table).
And then, you have your db.layouts.find(<filter>, <projection>) operation.

Since you’re always matching the same document (row) with <filter> object, you will always have same result. With <projection> object, you can select, what properties of the document (columns in the row) to show (1) or hide (0).

When you specify { ‘info.married’: 1 } in the <projection> object, you decide to show the nested property (column of the column) ‘info.married’ in the output.

I recommend you to take some courses on MongoDB. At least, the very basic one.

Hi Slava, after all thanks a lot for your help and overall for reedit my post. As you can see in this line " db.layouts.find({_id: “clients”, “info.married”: 1}, {“info.id”: 1, “info.married”: 1, _id: 0}) " info.married: 1 is in both (filter & projections), my question would be, Can’t have the column in filter and projections at same time or where is the mistake ?

<filter> and <projection> objects can have same properties. It is not the issue.

With <filter> you decide to get this one document (row) for your collection (table)

{
  '_id': 'clients',
  'info' : [
    { 'id' : 'luis.alvarez', 'married' : 1 },
    { 'id' : 'jose.perez', 'married' : 0 },
    { 'id' : 'daniel.contreras', 'married' : 0 },
    { 'id' : 'javier.pirela', 'married' : 0 }
  ]
}

And with <projection> you can only show or hide some fields (columns) in the above document (row);
Notice: show / hide, but not filter.

So, if you specify { ‘info.married’: 0 } in your <projection> object, you just hide all the props ‘married’, and this’s it. You will get the same document, but ‘info.married’ prop will be hidden:

{
  '_id': 'clients',
  'info' : [
    { 'id' : 'luis.alvarez' },
    { 'id' : 'jose.perez' },
    { 'id' : 'daniel.contreras' },
    { 'id' : 'javier.pirela' }
  ]
}

Before filtering nested properties (and ‘married’ is a nested property), you need to learn how simple things are done.

Thanks a lot again Slava, I mean that my problem is with MongoDB (I began yesterday to work with MongoDB), I’ve much experience con OOP, Class, Objects, methods & properties, and overall with json files (nested and very complex) with Phyton, PHP, Javascript.
I’m not sure if you saw well the filter (I took a example directly from MongoDB page for nested json) , because filter contains the field “info.married”: 1, and I can’t see the problem
I’m very clear how do you mention that is a process of “change mind” for to work with MongoBD as Database.
Once more time, thanks a lot for your help & your time.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.