简体   繁体   English

如何从文档中删除属性-Mongo / Loopback

[英]How to remove a property from document - mongo / loopback

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: 我尝试通过/ upsertWithWhere发布,其中使用ID来更新:

  • passing in undefined for the value which results in "http error 400 bad request" 传递未定义的值,导致“ http错误400错误的请求”
  • passing in null which just sets the property to null 传入null,它将属性设置为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. 我还认为我可以执行常规的POST并只覆盖每个文档,但是这些特定的文档很大,因此我不愿意这样做。

Is there a way to simply patch it? 有没有办法简单地修补它?

Edit: Need an answer that implements this Via the Loopback API. 编辑:需要一个通过Loopback API实现此目的的答案。

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. 显然,只需确保将“旧属性”的字段名称插入“ old_property”即可。

Explaining the query a little further... 进一步解释查询...

  1. "{}" matches all documents in the collection “ {}”与集合中的所有文档匹配
  2. "{$unset: {"old_property": ""}" removes the field(s) specified “ {$ unset:{” old_property“:”“}”删除指定的字段
  3. "{multi:true}" (An optional field for update) Allows you to update multiple documents when set to true “ {multi:true}”(用于更新的可选字段)设置为true时,允许您更新多个文档

Used this as a reference: https://docs.mongodb.com/manual/reference/method/db.collection.update/#multi-parameter 将此用作参考: https : //docs.mongodb.com/manual/reference/method/db.collection.update/#multi-parameter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM