0

I am having a json file containing the following typ of data:

{"_id":{"$oid":"5913ff31cb98a111ec26c4be"},"email":"abc@example.com","src":[{"acc":"ln"},{"acc":"mysp"},{"acc":"beta","login":"seller","lang":"english"}]}
{"_id":{"$oid":"5913ff31cb98a111ec26c4bf"},"email":"francescoseccia@example.com","src":[{"acc":"ln"}]}
{"_id":{"$oid":"5913ff31cb98a111ec26c4c0"},"email":"paulcohenlaw@example.com","src":[{"acc":"ln"}]}
{"_id":{"$oid":"5913ff31cb98a111ec26c4c1"},"email":"david.martinez_01@example.com","src":[{"acc":"ln"}]}
{"_id":{"$oid":"5913ff31cb98a111ec26c4c2"},"email":"sodanoa@example.it","src":[{"acc":"ln"}]}
{"_id":{"$oid":"5913ff31cb98a111ec26c4c3"},"email":"boyhoxiy@example.com","src":[{"acc":"ln"}]}
{"_id":{"$oid":"5913ff31cb98a111ec26c4bf"},"email":"FRancescoseccia@example.com","src":[{"acc":"ln","login":"in"},{"acc":"mysp","login":"beta"},{"acc":"db","login":"beta"}]}

These are json values.

I am trying to insert the values in MongoDB using pymongo library. I want to insert the value in the following way:

{
        "_id" : ObjectId("592402db50409d7e8d94fae4"),
        "email" : "abc@example.com",
        "src" : [
                {
                        "acc" : "ln"
                },
                {
                        "acc" : "mysp"
                },
                {
                        "acc" : "beta"
                }
        ],
        "login" : [
                "seller"
        ],
        "lang" : [
                "english"
        ]
}
{
        "_id" : ObjectId("592402eb50409d7e8d94fae5"),
        "email" : "francescoseccia@example.com",
        "src" : [
                {
                        "acc" : "ln"
                }
        ]
}
{
        "_id" : ObjectId("592402f850409d7e8d94fae6"),
        "email" : "paulcohenlaw@example.com",
        "src" : [
                {
                        "acc" : "ln"
                }
        ]
}
{
        "_id" : ObjectId("5924030550409d7e8d94fae7"),
        "email" : "david.martinez_01@example.com",
        "src" : [
                {
                        "acc" : "ln"
                }
        ]
}
{
        "_id" : ObjectId("5924031150409d7e8d94fae8"),
        "email" : "sodanoa@example.it",
        "src" : [
                {
                        "acc" : "ln"
                }
        ]
}
{
        "_id" : ObjectId("5924031c50409d7e8d94fae9"),
        "email" : "boyhoxiy@example.com",
        "src" : [
                {
                        "acc" : "ln"
                }
        ]
}
{
        "_id" : ObjectId("5924032850409d7e8d94faea"),
        "email" : "FRancescoseccia@example.com",
        "src" : [
                {
                        "acc" : "ln"
                },
                {
                        "acc" : "mysp"
                },
                {
                        "acc" : "db"
                }
        ],
        "login" : [
                "in",
                "beta"
        ]
}

Just need to keep the acc' in thesrc` set. And if there are any other values, that nee to be outside in the separate set.
Kindly, let me know who I can do that using pymongo.

Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139
  • Why not make `src` an embedded document where `acc` is an array field? I think it will make your life easier unless the field is dynamic which is a bad idea. – styvane May 23 '17 at 10:04
  • @S.M.Styvane sorry but didn't got your point, my friend. PLease can you tell me more about it? – Jaffer Wilson May 23 '17 at 10:09
  • What I meant is, you should change your document structure to this: `{"email" : "boyhoxiy@example.com", "src" : ["ln" ]}` – styvane Jun 02 '17 at 07:48

0 Answers0