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!

  • Why? And you might want to `find` not to `update` ... – Jonas Wilms Feb 25 '18 at 12:39
  • When I use find I get the following response: `{ MongoError: Unsupported projection option: $pull: { tables.$.groups: null }` –  Feb 25 '18 at 13:10
  • Whats your usecase? What are you trying to do with all of this? – Jonas Wilms Feb 25 '18 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 –  Feb 25 '18 at 13:20
  • Now i get you. `update({"tables.groups": null}` that only updates groups were the groups actually dont exist, so you might do `update({}` to apply it to all documents – Jonas Wilms Feb 25 '18 at 13:34
  • Sorry, my question was not formulated well I think. If I make your suggested changes I get the following response: `{ MongoError: The positional operator did not find the match needed from the query. Unexpanded update: tables.$.groups` –  Feb 25 '18 at 13:45
  • 1
    Oh right and `updateMany` instead of `update` – Jonas Wilms Feb 25 '18 at 13:53
  • Thanks for the answer, I am using monogjs package, there is no `updateMany`. Probably I need to do some changes. –  Feb 25 '18 at 14:09
  • Without changing from mongojs I found a solution: I queried the collection with `db.collection.find()`, then I saved the documents in an temporary array via a callback. I made the changes in the array, removed the documents from the collection `db.collection.remove()` and then I saved the array to the collection as new documents `db.collection.save(tablesTemp3[0])` –  Feb 27 '18 at 15:37
  • Ok. That sounds more complicated than i thought it would be. However maybe others will have the same problem, so feel free to add an answer to this to help others :) – Jonas Wilms Feb 27 '18 at 18:11
  • Great, I hope it is a good approach to solve the problem. Thanks for the help! –  Feb 27 '18 at 18:33

1 Answers1

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])