如何从文档中删除属性-Mongo/Loopback [英] How to remove a property from document - mongo / loopback

查看:130
本文介绍了如何从文档中删除属性-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...

  1. "{}"匹配集合中的所有文档
  2. "{$ unset:{" old_property:"}"删除指定的字段
  3. "{multi:true}"(用于更新的可选字段)设置为true时,允许您更新多个文档

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

这篇关于如何从文档中删除属性-Mongo/Loopback的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆