简体   繁体   中英

How to Get Data using Mongoose when Multilingual is stored in collections

I have saved data in mongo db like

{
     "title": {
                "ar": "arabic name",
                "en": "english name"

            },
     "description": {
                "ar": "ar-description",
                "en": "arabic description"
            },
     "subCategories": [{ 
               "title": {
                    "ar": "arabic name",
                    "en": "english name"
            }],
 }

Expected OutPut :

    {
     "title": "arabic name",
     "description": "arabic description",
     "subCategories": [{"title": "arabic name"}],
    }

How can i query to mongoose to give this result on basis of language(en,ar). i am using mongoose, nodejs and express. if cant with mongoose then any other solution? Thank you:)

Best way to do would be storing an unwinded document

{
 "title": {
            "ar": "arabic name",
            "en": "english name"

        },
 "description": {
            "ar": "ar-description",
            "en": "arabic description"
        },
 "subCategories": [{ 
           "title": {
                "ar": "arabic name",
                "en": "english name"
        }],
 }

will become

// ar doc
{
 "lang": "ar",
 "title": "Arabic name",
 "description": "ar-description",
 "subCategories": [{ 
           "title": "arabic name"
        }],
 }
 // en doc
 {
 "lang": "en",
 "title": "English name",
 "description": "en-description",
 "subCategories": [{ 
           "title": "english name"
        }],
 }

In order to find an ar doc

Model.find({lang:'ar'})

would give you the expected result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM