How to remove a property from document - mongo / loopback

这一生的挚爱 提交于 2019-12-12 01:46:14

问题


I have an old property on one of my models, and I'd like to remove it from all the documents in a collection. I've tried posting via /upsertWithWhere using the id to update by:

  • passing in undefined for the value which results in "http error 400 bad request"
  • passing in null which just sets the property to null

I also was thinking I could do a regular POST and just overwrite each document, but these particular documents are large and I'd rather not do that.

Is there a way to simply patch it?

Edit: Need an answer that implements this Via the Loopback API.


回答1:


This query should do the trick:

db.collection('collection_name').update({},{$unset: {"old_property": ""}}, {multi:true})

Obviously, just make sure you insert into "old_property" the field name of that old property.

Explaining the query a little further...

  1. "{}" matches all documents in the collection
  2. "{$unset: {"old_property": ""}" removes the field(s) specified
  3. "{multi:true}" (An optional field for update) Allows you to update multiple documents when set to true

Used this as a reference: https://docs.mongodb.com/manual/reference/method/db.collection.update/#multi-parameter



来源:https://stackoverflow.com/questions/42448546/how-to-remove-a-property-from-document-mongo-loopback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!