Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

mongodb - query an array of object in an array of object

I have this document model below. I want to query by attributes with name and value (I know the name and value of attribute eg. {"name": "Renk", "value": "Kirmizi"}). All I need is the document. I dont need index of variant or attribute, just document's itself. My purpose is not to get attributes or variants. I am querying across all documents. I hope you don't understand me wrong

{
    "_id" : ObjectId("5febdbf5d71b7ca4eef06cc5"),
    "variants" : [
        {
            "id" : "xtb2E3PVNitmh5xhuRAFNd",
            "attributes" : [
                {
                    "name" : "Renk",
                    "value" : "Kirmizi"
                },
                {
                    "name" : "Beden",
                    "value" : "36"
                },
                {
                    "name" : "Yaka Tipi",
                    "value" : "V Yaka"
                }
            ]
        },
        {
            "id" : "aP6CDPsgVLxTLgY5D6bTm9",
            "attributes" : [
                {
                    "name" : "Renk",
                    "value" : "Kirmizi"
                },
                {
                    "name" : "Beden",
                    "value" : "38"
                },
                {
                    "name" : "Yaka Tipi",
                    "value" : "V Yaka"
                }
            ]
        },
        {
            "id" : "b3mLCJXe6ae8HhNAY8dCPW",
            "attributes" : [
                {
                    "name" : "Renk",
                    "value" : "Mavi"
                },
                {
                    "name" : "Beden",
                    "value" : "38"
                },
                {
                    "name" : "Yaka Tipi",
                    "value" : "V Yaka"
                }
            ]
        },
        {
            "id" : "hYrANxJTbXmEYDUjtFpEKT",
            "attributes" : [
                {
                    "name" : "Renk",
                    "value" : "Mavi"
                },
                {
                    "name" : "Beden",
                    "value" : "36"
                },
                {
                    "name" : "Yaka Tipi",
                    "value" : "V Yaka"
                }
            ]
        }
    ],
    "updatedDate" : NumberLong(1609292789)
}

Any ideas? Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This should work. This will search exact key and value pair :

 {'variants.attributes': { 'name' : 'Renk', 'value' : 'Kirmizi' }}

Result on MongoDB Compass

Note: Be careful when do this { "variants.attributes.name": "Renk", "variants.attributes.value": "Kirmizi" }. The object is flatten so this query means find any document contains key 'Renk' and value 'Kirmizi' but 'Renk' and 'Kirmizi' can be in different objects!

Read official document from Mongo for more details https://docs.mongodb.com/manual/tutorial/query-array-of-documents/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...