0

When I run mongdob query I got this json results in a column. Column name is "user"

{"email": "mail1@mail.com"}
{"email": "mail6@mail6.com", "name": "name1"}
{"email": "mail2@mail.com"}
{"email": "mail7@mail7.com", "name": "mail2"}
{"email": "mail3@mail.com"}
{"email": "mail5@mail8.com", "name": "name3"}
{"email": "mail4@mail.com"}
{"email": "mail5@mail9.com", "name": "name4"}

However I need to extract email and name from this JSON into different columns like "email", "name"

How can I do this with Mongodb?

Output

enter image description here

0

Have you tried with aggregate yet?

Following I used $addField and $project

var c = db.getCollection("json_column")
if (c == null) {
    db.createCollection('json_column');
}
db.getCollection("json_column").remove({});

db.getCollection('json_column').insert({user:{"email": "mail6@mail6.com", "name": "name1"}});
db.getCollection('json_column').insert({user:{"email": "mail2@mail.com"}});
db.getCollection('json_column').insert({user:{"email": "mail7@mail7.com", "name": "mail2"}});
db.getCollection('json_column').insert({user:{"email": "mail3@mail.com"}});
db.getCollection('json_column').insert({user:{"email": "mail5@mail8.com", "name": "name3"}});
db.getCollection('json_column').insert({user:{"email": "mail5@mail9.com", "name": "name4"}});

db.getCollection('json_column').aggregate([
{
    $addFields: {
        "email": "$user.email",
        "name": "$user.name"
    }
},
{
    $project: {
        "user":0
    }
}
]);

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.