如何从mongodb中的文档中仅删除一个或两个字段? [英] How to remove only one or two fields from documents in mongodb?

查看:60
本文介绍了如何从mongodb中的文档中仅删除一个或两个字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常简单的问题,只是大脑结冰严重.在我的汇总中,我只想使用 $ project 删除'_id'字段,但返回其他所有内容.但是,我得到

This is a very easy question, just having a really bad brain freeze. In my aggregation, I just want to remove the '_id' field by using $project but return everything else. However, I'm getting

$ projection至少需要一个输出字段

$projection requires at least one output field

我认为这就像:

db.coll.aggregate( [ { $match .... }, { $project: { _id: 0 }}])

推荐答案

使用聚合时,您需要通过各种管道操作或通过

You need to explicitly include fields when using aggregation either via various pipeline operations or via $project. There currently isn't a way to return all fields unless explicitly defined by field name:

$project : {
   _id : 0,
   "Name" : 1,
   "Address" : 1
}

您可以使用所使用的技术排除 _id ,如上所示.

You can exclude the _id using the technique you used and as shown above.

这篇关于如何从mongodb中的文档中仅删除一个或两个字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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