2

I have a JSON object in my MONGODB

{
"_id" : ObjectId("59d4b9848621854d8fb2b1e1"),
"Bot_name" : "Scheduling bot",
"Modules" : [ 
    {
        "ModuleID" : "1111",
        "ModuleStatement" : "This is a Sceduling bot, Would you like to book a flight?",
        "_id" : ObjectId("59d4b9968621854d8fb2b1e3"),
        "ModuleResponse" : [ 
            {
                "Response" : "yes",
                "TransBotID" : "1112"
            }, 
            {
                "Response" : "no",
                "TransBotID" : "1113"
            }
        ]
    }, 
    {
        "ModuleID" : "1112",
        "ModuleStatement" : "Where would you like to go? New York ? LA?",
        "_id" : ObjectId("59d4b9968621854d8fb2b1e3"),
        "ModuleResponse" : [ 
            {
                "Response" : "New York",
                "TransBotID" : "1121"
            }, 
            {
                "Response" : "LA",
                "TransBotID" : "1122"
            }
        ]
    }, 
    {
        "ModuleID" : "1121",
        "ModuleStatement" : " New York..",
        "_id" : ObjectId("59d4b9968621854d8fb2b1e3"),
        "ModuleResponse" : []
    }, 
     {
        "ModuleID" : "1121",
        "ModuleStatement" : " New York..",
        "_id" : ObjectId("59d4b9968621854d8fb2b1e3"),
        "ModuleResponse" : []
      }
   }

Im making a query that will first check the Bot_name and then check the ModuleID which is in the nested array Modules containing JSON object which are 1111, 1112 , 1121 .. so on how do i only get the json object of ModuleID:1111 of Bot_name:Scheduling bot

so far my query is

botSchema.findOne({ Bot_name: req.body.Name ,'Modules.ModuleID':req.body.MID}, function (err, data) {
console.log(data)
   }

here the query returns all the json inside the Modules

how to only get one desired json object? like this

{
    "ModuleID" : "1111",
    "ModuleStatement" : "This is a Sceduling bot, Would you like to book a flight?",
    "_id" : ObjectId("59d4b9968621854d8fb2b1e3"),
    "ModuleResponse" : [ 
        {
            "Response" : "yes",
            "TransBotID" : "1112"
        }, 
        {
            "Response" : "no",
            "TransBotID" : "1113"
        }
    ]
}
3
  • Your query ('Modules.ModuleID: req.body.MID) should be working and filtering the Modules array. So technically data.Modules[0] should be what you want. Commented Oct 7, 2017 at 11:29
  • doesn't work . it returns all the Modules json object Commented Oct 7, 2017 at 13:47
  • it works if i use for loops but it would be easier to query and get the exact json i want . is that possible or we can only get all of the object as my schema? Commented Oct 7, 2017 at 13:48

1 Answer 1

2

You need to use $elemMatch for filter sub arrays.

db.botSchema.findOne( 
    { Bot_name: "Scheduling bot"} 
    , { 'Modules': { $elemMatch:{'ModuleID':"1111"} } }
    , function (err, data) { console.log(data) })

Result:

{
    "_id" : ObjectId("59d4b9848621854d8fb2b1e1"),
    "Modules" : [ 
        {
            "ModuleID" : "1111",
            "ModuleStatement" : "This is a Sceduling bot, Would you like to book a flight?",
            "_id" : ObjectId("59d4b9968621854d8fb2b1e3"),
            "ModuleResponse" : [ 
                {
                    "Response" : "yes",
                    "TransBotID" : "1112"
                }, 
                {
                    "Response" : "no",
                    "TransBotID" : "1113"
                }
            ]
        }
    ]
}
Sign up to request clarification or add additional context in comments.

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.