1

How can I pull from the following MongoDB document all null values in, so that there are no more null values in the groups array? I want to do it in NodeJS.

(In my real document I have more embedded documents in the tables array with the same structure)

{
    "_id": {
        "$oid": "5a4267b9734d1d5b99268cbe"
    },
    "department": "waeldlerStubeKristallStube",
    "tables": [
        {
            "arrayIndex": 0,
            "department": "waeldlerStubeKristallStube",
            "number": "50",
            "topValue": "139",
            "leftValue": "455",
            "bgColor": "#ffffff",
            "isBesetzt": "false",
            "placeholder": "true",
            "border": "solid 3px #f3efe4",
            "width": "40",
            "height": "35",
            "groups": [
            null,
                {
                    "nameValue": "Hunziker, Iveta",
                    "zimmernummerValue": "460",
                    "anreiseValue": "16.02.2018",
                    "abreiseValue": "25.02.2018",
                    "personenAnzahlValue": "2/1",
                    "notiz2Value": "2*50 Euro Spa Taler",
                    "notiz1Value": "2 Erw neu + 1 Baby geänderte reservierung neues Datum",
                    "traceValue": "-",
                    "bemerkungValue": "-"
                },
            null
            ]
        }
]
}

It seems to me that I need to update the document, query all the groups with the value null, which is in the tables array and pull the null with

tables.$.groups

including a positional operator. I had also a look at the following post: MongoDB pull element from array two levels deep and came up with this try:

db.hubertusTables.update({"tables.groups": null},{ $pull: { "tables.$.groups": null} }, { multi: true});

My try does not work. I get the following response:

{"n":2,"nModified":0,"opTime":{"ts":"6526469489381343253","t":4},"electionId":"7fffffff0000000000000004","ok":1}

Thanks a lot for the effort!

11
  • Why? And you might want to find not to update ... Commented Feb 25, 2018 at 12:39
  • When I use find I get the following response: { MongoError: Unsupported projection option: $pull: { tables.$.groups: null } Commented Feb 25, 2018 at 13:10
  • Whats your usecase? What are you trying to do with all of this? Commented Feb 25, 2018 at 13:14
  • I want to $pull from the above given MongoDB document all null values, so that there are no more null values in the groups array? This should be done in NodeJS Commented Feb 25, 2018 at 13:20
  • 1
    Oh right and updateMany instead of update Commented Feb 25, 2018 at 13:53

1 Answer 1

1

Without changing from mongojs I found a solution: I queried the collection with db.collection.find(), then I saved the documents in a temporary array via a callback. I made the changes in the array, removed the documents from the collection with db.collection.remove() and then I saved the array to the collection as new documents db.collection.save(tablesTemp[0])

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.