Mongoose and array elements

73 views
Skip to first unread message

Satish B

unread,
Jul 20, 2015, 11:25:13 AM7/20/15
to nod...@googlegroups.com
Document:
 User collection
{
        'name': 'abc',
        "watch" : [
                {
                        "cat_name" : "Satish",
                        "cat_id" : "55ace5047f80b77017edsfa0e",
                        "script" : {
                                "script" : "SBIN",
                                "exchange" : "NS"
                        }
                },
                {
                        "cat_name" : "Satish",
                        "cat_id" : "55ace5047f8sdf7017ed8a0e",
                        "script" : {
                                "script" : "ITC",
                                "exchange" : "NS"
                        }
                },
                {
                        "cat_name" : "technotip",
                        "cat_id" : "55ace590htt0b77017ed8a10",
                        "script" : {
                                "script" : "SYNCOM",
                                "exchange" : "NS"
                        }
                }
        ]
}

Now how can I fetch only the object cat_name = technotip

User.find({'name': 'abc'}, {});
This gets me the whole document. I only want the object which is inside key 'watch',  with 'cat_name' as ''technotip'.

Please help.


If I had simple values inside the array I would have used comparison operator to fetch the value, but in this case I have objects inside the array.

Pablito Ron

unread,
Jul 28, 2015, 10:07:28 AM7/28/15
to nodejs, technotip...@gmail.com
I can't recall the name but mongoDB allows you to find by looking at arrays values, but it will always return the whole document I think you'll have to add the logic to return the part of the document you're interested in.
Reply all
Reply to author
Forward
0 new messages