0

Here is my JSON structure for documents in GoogleBooks collection:

{
        "_id" : ObjectId("5a35603adc354d05d03c121b"),
        "kind" : "books#volume",
        "id" : "R2daemCCiF8C",
        "etag" : "oZeKglm+ChI",
        "selfLink" : "https://www.googleapis.com/books/v1/volumes/R2daemCCiF8C",
        "volumeInfo" : {
                "title" : "Harry Potter y el cáliz de fuego",
                "authors" : [
                        "J.K. Rowling"
                ],
                "publisher" : "Pottermore",
                "publishedDate" : "2015-12-08",
                "description" : "Otro deplorable verano con los Dursley llega a su fin y Harry está impaciente por regresar a Hogwarts. A sus catorce años, sólo desea ser un joven mago como los demás y dedicarse a aprender nuevos sortilegios y asistir a los Mundiales de quidditch. Sin embargo, en Hogwarts le espera un desafío de grandes proporciones, por lo que tendrá que demostrar que ya no es un niño y que está preparado para vivir las nuevas y emocionantes experiencias que el futuro le depara.",
                "industryIdentifiers" : [
                        {
                                "type" : "ISBN_13",
                                "identifier" : "9781781102701"
                        },
                        {
                                "type" : "ISBN_10",
                                "identifier" : "1781102708"
                        }
                ],
                "readingModes" : {
                        "text" : true,
                        "image" : true
                },
                "pageCount" : 672,
                "printType" : "BOOK",
                "categories" : [
                        "Juvenile Fiction"
                ],
                "averageRating" : 4.5,
                "ratingsCount" : 22,
                "maturityRating" : "NOT_MATURE",
                "allowAnonLogging" : true,
                "contentVersion" : "1.4.4.0.preview.3",
                "panelizationSummary" : {
                        "containsEpubBubbles" : false,
                        "containsImageBubbles" : false
                }...}
{
        "_id" : ObjectId("5a35603adc354d05d03c121c"),
        "kind" : "books#volume",
        "id" : "2zgRDXFWkm8C",
        "etag" : "N5pBavgCXy0",
        "selfLink" : "https://www.googleapis.com/books/v1/volumes/2zgRDXFWkm8C",
        "volumeInfo" : {
                "title" : "Harry Potter y la piedra filosofal",
                "authors" : [
                        "J.K. Rowling"
                ],
                "publisher" : "Pottermore",
                "publishedDate" : "2015-12-08",
                "description" : "Harry vive con sus horribles tíos y el insoportable primo Dudley, hasta que su ingreso en el Colegio Hogwarts de Magia y Hechicería cambia su vida para siempre. Allí aprenderá trucos y encantamientos fabulosos, y hará un puñado de buenos amigos... aunque también algunos temibles enemigos. Y, sobre todo, conocerá los secretos que lo ayudarán a cumplir con su destino.",
                    "industryIdentifiers" : [
                            {
                                    "type" : "ISBN_10",
                                    "identifier" : "9587155017"
                            },
                            {
                                    "type" : "ISBN_13",
                                    "identifier" : "9789587155013"
                            }
                ],
                "readingModes" : {
                        "text" : true,
                        "image" : true
                },
                "pageCount" : 264,
                "printType" : "BOOK",
                "categories" : [
                        "Juvenile Fiction"
                ],
                "averageRating" : 4,
                "ratingsCount" : 95,
                "maturityRating" : "NOT_MATURE",
                "allowAnonLogging" : true,
                "contentVersion" : "1.5.6.0.preview.3",
                "panelizationSummary" : {
                        "containsEpubBubbles" : false,
                        "containsImageBubbles" : false
                }...}
}

I´m trying to drop ISBN_10 for each document to leave only ISBN_13, in other words I want to delete the type and the identifier of ISBN_10. The result would be:

...
"industryIdentifiers" : [
                        {
                                "type" : "ISBN_13",
                                "identifier" : "9781781102701"
                        }
                ],
...

I´m trying like this but I think the syntax is not correct:

db.GoogleBooks.find({}).forEach(function(doc) {
    var isbn = doc.volumeInfo.industryIdentifiers;
        for(var i = 0; i < isbn.length; ++i) {
            var x = isbn[i];
            if(x !== null && x["type"] === "ISBN_10"){
                delete (x);
            }
        }
    db.GoogleBooks.save(doc);
}); 

Any ideas? Thanks in advance.

  • 2
    You can use `$pull`. `db.GoogleBooks.update( { }, { $pull: { "volumeInfo.industryIdentifiers.type":"ISBN_13" } }, { multi: true }` )` – s7vr Dec 17 '17 at 12:53
  • 1
    Possible duplicate of [How to remove array element in mongodb?](https://stackoverflow.com/questions/16959099/how-to-remove-array-element-in-mongodb) – s7vr Dec 17 '17 at 12:54

0 Answers0