0

I am using Loopback 3 and mongoDB

I add new field with default value in json model

  {
    "name": "myModel",
    "options": {
        "mongodb": {
          "collection": "MyModel"
        }
    },
    "mixins": {
       "ShareApi": {
        "allow": ["findById"]
      }
    },
    "properties": {
      "name": {
        "type": "string",
        "description": "name"
      },
      "newField": {
        "type": "boolean",
        "default": false
      }
    }
  }

However, this just work for new documents. How to old documents are updated with newfield

4
  • You can do it with an aggregation pipeline update, $set and $ifNull. Commented Jul 13, 2020 at 2:57
  • @D.SM does Loopback support this migration? I want to achieve this from Loopback, not mongoDB Commented Jul 13, 2020 at 3:53
  • Don't know about that. Commented Jul 13, 2020 at 4:13
  • Does this answer your question? MongoDB: update every document on one field Commented Jul 13, 2020 at 13:31

1 Answer 1

0

If you want to update all documents then use updateAll query of loopback:

myModel.updateAll({}, { newField: true }, function (err, affected) { })
                            
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.