如何从文档中删除属性-Mongo/Loopback [英] How to remove a property from document - mongo / loopback
问题描述
我的一个模型中有一个旧属性,我想将其从集合中的所有文档中删除.我尝试过通过/upsertWithWhere使用ID进行更新的发布方式:
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:
- 传入未定义的值,导致出现"http错误400错误的请求"
- 传入null,它将属性设置为null
我还认为我可以执行常规的POST并只覆盖每个文档,但是这些特定的文档很大,因此我不愿意这样做.
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.
是否可以简单地对其进行修补?
需要一个通过Loopback API实现此目的的答案.
Need an answer that implements this Via the Loopback API.
推荐答案
此查询应能解决问题:
db.collection('collection_name').update({},{$unset: {"old_property": ""}}, {multi:true})
很显然,只需确保将旧属性"的字段名称插入"old_property"中即可.
Obviously, just make sure you insert into "old_property" the field name of that old property.
进一步解释查询...
Explaining the query a little further...
- "{}"匹配集合中的所有文档
- "{$ unset:{" old_property:"}"删除指定的字段
- "{multi:true}"(用于更新的可选字段)设置为true时,允许您更新多个文档
将此用作参考: https: //docs.mongodb.com/manual/reference/method/db.collection.update/#multi-parameter
这篇关于如何从文档中删除属性-Mongo/Loopback的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!