0

i have below mongoDB collection data. i am trying to fetch data using typesOfExicution, companyName and projectName. i have wrote the below query for finding

db.getCollection('SETTINGS').find({'companyName':'VV', 'typesOfExicution' : 'validation', 'automation.projectName' : 'hdh'})

if i don't use automation.projectName i am able to find the data. but if i use this then it is returning 0 records.

so what is the exact query to find data from an Array.

/* 1 */
{
    "_id" : ObjectId("5abce315cabca22270eead6a"),
    "typesOfExicution" : "validation",
    "modesOfExicution" : "auto",
    "automation[0][projectName]" : "Second",
    "automation[0][modules][]" : "Second module",
    "automation[0][assets][]" : "Second assets",
    "companyName" : "VV"
}

/* 2 */
{
    "_id" : ObjectId("5abce31ccabca22270eead6b"),
    "typesOfExicution" : "validation",
    "modesOfExicution" : "auto",
    "automation[0][projectName]" : "Second",
    "automation[0][modules][]" : "Second module",
    "automation[0][assets][]" : "Second assets",
    "companyName" : "VV"
}

/* 3 */
{
    "_id" : ObjectId("5abce321cabca22270eead6c"),
    "typesOfExicution" : "validation",
    "modesOfExicution" : "auto",
    "automation[0][projectName]" : "hdh",
    "automation[0][modules][]" : "ds",
    "automation[0][assets][]" : "djv",
    "companyName" : "VV"
}
2
  • To search from an Array you would do the same as it was another field. Like: "automation.projectName: "Second", "automation.modules": "Second Module". If you need multiple conditions from the same Array, use $elemMatch Commented Mar 29, 2018 at 13:17
  • @Sangram Badi, check the answer posted. Commented Mar 29, 2018 at 14:30

2 Answers 2

2

This is not how you should store arrays in MongoDB. You should store array elements as embedded documents. As @Alexis mentioned here, there's no clarity on what exactly is your DB collection structure, so I hope this is what expected as per your given inputs:

/* 1 */
{
"_id" : ObjectId("5abce315cabca22270eead6a"),
"typesOfExicution" : "validation",
"modesOfExicution" : "auto",
"automation" : [ 
    {
        "projectName" : "Second",
        "modules" : [ 
            "Second module"
        ],
        "assets" : [ 
            "Second assets"
        ]
    }
],
"companyName" : "VV"
}

/* 2 */
{
"_id" : ObjectId("5abce31ccabca22270eead6b"),
"typesOfExicution" : "validation",
"modesOfExicution" : "auto",
"automation" : [ 
    {
        "projectName" : "Second",
        "modules" : [ 
            "Second module"
        ],
        "assets" : [ 
            "Second assets"
        ]
    }
],
"companyName" : "VV"
}

/* 3 */
{
"_id" : ObjectId("5abce321cabca22270eead6c"),
"typesOfExicution" : "validation",
"modesOfExicution" : "auto",
"automation" : [ 
    {
        "projectName" : "hdh",
        "modules" : [ 
            "ds"
        ],
        "assets" : [ 
            "djv"
        ]
    }
],
"companyName" : "VV"
}

Once you have the data as per mentioned above, use automation.projectName and you should get results like this:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

I'm not very sure of how your stored document is structured. I believe it looks something like:

   {
      "typesOfExicution": string,
      "modesOfExicution": string,
      "automation": [{ projectName: string, modules: string, assets: string }]
      "companyName" : string
   }

If you want to perform a query over projectName, which belongs to objects composing the array automation, you need to use the $elemMatch operator:

   db.getCollection('SETTINGS').find({'companyName':'VV', 'typesOfExicution' : 'validation', 'automation' : { $elemMatch: { 'projectName': 'myProject' } }  })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.