mongodb 包括所有现有字段并将新字段添加到文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19431773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:27:24  来源:igfitidea点击:

Include all existing fields and add new fields to document

mongodbmongodb-queryaggregation-framework

提问by samuelluis

I would like to define a $project aggregation stage where I can instruct it to add a new field and include all existing fields, without having to list all the existing fields.

我想定义一个 $project 聚合阶段,我可以指示它添加一个新字段并包含所有现有字段,而不必列出所有现有字段。

My document looks like this, with many fields:

我的文档看起来像这样,有很多字段:

{
    obj: {
        obj_field1: "hi",
        obj_field2: "hi2"
    },
    field1: "a",
    field2: "b",
    ...
    field26: "z"
}

I want to make an aggregation operation like this:

我想进行这样的聚合操作:

[
    {
        $project: {
            custom_field: "$obj.obj_field1",
            //the next part is that I don't want to do
            field1: 1,
            field2: 1,
            ...
            field26: 1
        }
    },
    ... //group, match, and whatever...
]

Is there something like an "include all fields" keyword that I can use in this case, or some other way to avoid having to list every field separately?

在这种情况下,是否可以使用诸如“包含所有字段”之类的关键字,或者可以通过其他方式避免必须单独列出每个字段?

回答by styvane

In 4.2+, you can use the $setaggregation pipeline operator which is nothing other than an alias to $addFieldsadded in 3.4

在 4.2+ 中,您可以使用$set聚合管道运算符,它只不过是$addFields在 3.4 中添加的别名

The $addFieldsstage is equivalent to a $projectstage that explicitly specifies all existing fields in the input documents and adds the new fields.

$addFields阶段相当于$project明确指定输入文档中所有现有字段并添加新字段的阶段。

db.collection.aggregate([
    { "$addFields": { "custom_field": "$obj.obj_field1" } }
])

回答by Deka

You can use $$ROOTto references the root document. Keep all fields of this document in a field and try to get it after that (depending on your client system: Java, C++, ...)

您可以使用$$ROOT来引用根文档。将此文档的所有字段保留在一个字段中,然后尝试获取它(取决于您的客户端系统:Java、C++、...)

 [
    {
        $project: {
            custom_field: "$obj.obj_field1",
            document: "$$ROOT"

        }
    },
    ... //group, match, and whatever...
]

回答by Victoria Malaya

>>> There's something like "include all fields" keyword that I can use in this case or some another solution?

>>> 在这种情况下我可以使用诸如“包含所有字段”之类的关键字或其他解决方案?

Unfortunaly, there is no operator to "include all fields" in aggregation operation. The only reason, why, because aggregation is mostly created to group/calculate data from collection fields (sum, avg, etc.) and return all the collection's fields is not direct purpose.

不幸的是,在聚合操作中没有“包含所有字段”的运算符。唯一的原因,为什么,因为聚合主要是为了从集合字段(sum、avg 等)分组/计算数据并返回所有集合的字段而不是直接目的。

回答by Ghopper21

As of version 2.6.4, Mongo DB does not have such a feature for the $projectaggregation pipeline. From the docsfor $project:

从 2.6.4 版本开始,Mongo DB 没有这样的$project聚合管道功能。从文档$project

Passes along the documents with only the specified fields to the next stage in the pipeline. The specified fields can be existing fields from the input documents or newly computed fields.

将仅包含指定字段的文档传递到管道中的下一个阶段。指定的字段可以是输入文档中的现有字段或新计算的字段。

and

The _id field is, by default, included in the output documents. To include the other fields from the input documents in the output documents, you must explicitly specify the inclusion in $project.

_id 字段默认包含在输出文档中。要将输入文档中的其他字段包含在输出文档中,您必须在 $project 中明确指定包含内容。

回答by Deeksha Sharma

To add new fields to your document you can use $addFields

要将新字段添加到您的文档,您可以使用 $addFields

from docs

来自文档

and to all the fields in your document, you can use $$ROOT

对于文档中的所有字段,您可以使用 $$ROOT

db.collection.aggregate([

{ "$addFields": { "custom_field": "$obj.obj_field1" } },
{ "$group": {
        _id : "$field1",
        data: { $push : "$$ROOT" }
    }}
])

回答by mr.byte

according to @Deka reply, for c# mongodb driver 2.5 you can get the grouped document with all keys like below;

根据@Deka 回复,对于 c# mongodb 驱动程序 2.5,您可以获得包含所有键的分组文档,如下所示;

var group = new BsonDocument
{
 { "_id", "$groupField" },
 { "_document", new BsonDocument { { "$first", "$$ROOT" } } }
};

ProjectionDefinition<BsonDocument> projection = new BsonDocument{{ "document", "$_document"}};
var result = await col.Aggregate().Group(group).Project(projection).ToListAsync();

// For demo first record 
var fistItemAsT = BsonSerializer.Deserialize<T>(result.ToArray()[0]["document"].AsBsonDocument);