0

I have a collection like User having list user and that user have list of user. Like hierarchy.

   {        
     "_id" : ObjectId("55530326bc687d21783fd1ff"),
            "Name" : "User 1",
            "Role" : "Manager",
        number:NumberLong(0),
            "1" : 
        [
        {
        "_id" : ObjectId("55530326bc687d21783fd1fd"),
            "Name" : "User 2",
            "Role" : "Ass Manager",
        number:NumberLong(0),
            "1" : 
        [
        .......
        ]
        }
        {
        "_id" : ObjectId("55530326bc687d21783fd1fq"),
            "Name" : "User 2",
            "Role" : "Ass Manager",
    number:NumberLong(1),
            "1" : 
        [
        .........
        ]
        },
        {
        "_id" : ObjectId("55530326bc687d21783fd1fg"),
            "Name" : "User 3",
            "Role" : "Ass Manager",
    number:NumberLong(2),
            "1" : 
        [
        ........
        ]
        }
        ],
         "2" : 
        [
        {
        "_id" : ObjectId("55530326bc687d21783fd1fw"),
            "Name" : "User 4",
            "Role" : "Specialist",
        number:NumberLong(0),
            "1" : 
        [
        .......
        ]
        }
        {
        "_id" : ObjectId("55530326bc687d21783fd1fe"),
            "Name" : "User 5",
            "Role" : "Specialist",
    number:NumberLong(1),
            "1" : 
        [
        .........
        ]
        },
        {
        "_id" : ObjectId("55530326bc687d21783fd1fr"),
            "Name" : "User 6",
            "Role" : "Specialist",
    number:NumberLong(2),
            "1" : 
        [
        ........
        ]
        }
        ]
        }

The above is just one collection for sample, like this i have nearly 10000 document. I need to find the collection which have 'number' as 0. Even if any one embedded document have 'number' as 0 I want that document.

Note : I can't tell how many child will come for a user.

  • I think you need a bit of editing. You don't need to find "the collection", do you? You need to find documents. Moreover: do you need to find them, or to update them, as your title suggests? – Mario Trucco Aug 20 '15 at 07:24
  • I am sorry, If i know the find query, I can create update query myself. So I am asking for the find query. – Gopinath Navaneethan Aug 20 '15 at 10:13
  • This schema seems designed to be puzzling :) however, the number of elements in nested array is not going to be an issue, but it gets very hard if you can't tell the maximum level of embedding and the keys you have ("1", "2" here are not indexes, they are keys as well as "Name" and "Role") – Mario Trucco Aug 20 '15 at 12:14

1 Answers1

0

All right, I will assume that each of your User documents can have two arrays of children users (namely "1" and "2"), and that you have a maximum nesting level, say 3 (This means that a nested user cannot have more than 2 anchestors). By the way, the maximum nesting level allowed by mongodb is 100.

Probably this is not want you wanted: in this case you have issues with your schema design, because

Now, let's pretend that my assumptions were ok for you. Try (I'm calling your collection users since we don't usually capitalize collection names):

db.users.find({$or:["1.number" : 0, "2.number" : 0, "1.1.number":0, "1.2.number":0, ..., "2.2.1.number":0, "2.2.2.number":0]})

I have skipped some combinations that you need to add. Notice that you don't need to worry about the position in arrays, and that with only 3 levels of nesting there are already quite many clauses for the $or operator, and that will possibly convince you that it's better to follow the linked best practices.

NOTE for future readers: the OP has clarified in a comment that he doesn't actually need an update, but a find query.

Community
  • 1
  • 1
Mario Trucco
  • 1,933
  • 3
  • 33
  • 45
  • Thanks for this, I already going in the same way you told. Is there any generic way to find, otherwise i have to change my schema design. – Gopinath Navaneethan Aug 25 '15 at 06:56
  • The answer is no, you cannot find when you don't know the key names, and the nesting level. I really suggest changing the scema design – Mario Trucco Aug 25 '15 at 07:01