0

I have a small mongo document which looks like this:

{
    "_id": {
        "$oid": "5aa441e898cc0b32a819be0c"
    },
    "details": {
        "Artist": "Cyndi Lauper",
        "Album": "She's So Unusual",
        "ReleaseYear": 1983
    },
    "SongID": 1,
    "SongTitle": "Girls Just Want To Have Fund"
}

I need to pull the Artist element out of the details array but keep the data in the overall document. The resulting document needs to look like this:

{
    "_id": {
        "$oid": "5aa441e898cc0b32a819be0c"
    },
    "details": {
        "Album": "She's So Unusual",
        "ReleaseYear": 1983
    },
    "SongID": 1,
    "SongTitle": "Girls Just Want To Have Fund",
    "Artist": "Cyndi Lauper"
}

I'm new to mongo so I'm not to strong in this. The collection is called songs so am thinking I need to do something like this:

db.songs.updateOne({},{$set : {"Artist":$pull{"detail.Artist"}}})

Any help and/or suggestions are greatly appreciated.

Jonathan

Jonathan Small
  • 1,027
  • 3
  • 18
  • 40
  • I dont think my question is a duplicate. The other examples show how to remove an element from the array but in doing so, they loose the data in the removed element. I want to save the data. I just want to pull the array element called Artist out of the array so it will no longer be referenced with dot notation. – Jonathan Small Mar 11 '18 at 11:15

1 Answers1

0

You can write something like this in the MongoDB shell:

var doc={doc:{key42:"value"},somekey:"value"};
doc.key42=doc.doc.key42;
delete doc.doc.key42;

After that you can write

doc

and press enter. The output shold be:

{doc:{},somekey:"value",key42:"value"};

Then you can save the doc:

db.collection.save(doc);
neoexpert
  • 465
  • 1
  • 10
  • 20