How to search sub value

HI there,

Let said I’ve got a document structure like this:

  (
    [_id] => 5e7c65801760570981683461
    [RolesID] => apiUser
    [controller] => user
    [value] => Array
        (
            [0] => Array
                (
                    [sub] => login
                    [access] => Array
                        (
                            [0] => run
                        )

                )

            [1] => Array
                (
                    [sub] => logout
                    [access] => Array
                        (
                            [0] => run
                        )

                )

        )

)

What is the correct query syntax to find the collection for controller:user and sub:login ?

Thank you if someone can help me with this. Still new and learning about MongoDB

What is the correct query syntax to find the collection for controller:user and sub:login

In MongoDB document structure, from what you have posted, controller is a string field and sub is a string field in a nested (or sub or embedded) document of an array. Arrays and nested documents are compound data types, and the string is scalar.

Consider the following two documents in a test collection:

{ _id: 1, controller: "c-1", value: [ { sub: "login" }, { sub: "logout" } ] }
{ _id: 2, controller: "c-2", value: [ { sub: "login" }, { sub: "insert" }, { sub: "logout" } ] }

And the following queries:

db.test.find( { controller: "c-1" } )
db.test.find( { controller: "c-1", "value.sub": "login" } )
db.test.find( { "value.sub": "insert" } )

The first two queries return the document with _id: 1. The third query returns the document with _id: 2.

Here are some links to MongoDB documentation related to querying documents:


Still new and learning about MongoDB

I suggest you use the mongo shell and the MongoDB Compass tools to create and query documents; these are the most commonly used and understood.