mongodb 如何从聚合中隐藏_id?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15776453/
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
How to hide _id from Aggregation?
提问by Abdelouahab Pp
I've this query:
我有这个查询:
produits = yield motor.Op(db.users.aggregate, [{"$unwind":"$pup"},{"$match":{"pup.spec.np":nomp}}, {"$group":{"_id":"$pup.spec.id","pup":{"$push":"$pup"}}}])
which gives me this result:
这给了我这个结果:
print produits
{u'ok': 1.0, u'result': [{u'_id': None, u'pup': [{u'avt': {u'fto': ..all the results}}]}]}
so I can do:
所以我可以这样做:
prod = produits["result"]
[{u'_id': None, u'pup': [{u'avt': {u'fto': ..all the results}}]}]
but how can I hide "_id"
so that I can only get:
但我怎样才能隐藏,"_id"
以便我只能得到:
[{u'pup': [{u'avt': {u'fto': ..all the results}}]}]
in a normal query I would simply add something like {"_id":0}
but here it doesn't work.
在普通查询中,我会简单地添加类似的东西,{"_id":0}
但在这里它不起作用。
回答by sambomartin
From mongodb docs
来自 mongodb 文档
You can $project the results to exclude the _id
- is this what you mean?
您可以 $project 结果以排除_id
- 这是您的意思吗?
http://docs.mongodb.org/manual/reference/aggregation/#pipeline
http://docs.mongodb.org/manual/reference/aggregation/#pipeline
Note The _id field is always included by default. You may explicitly exclude _id as follows:
注意 _id 字段总是默认包含在内。您可以按如下方式明确排除 _id:
db.article.aggregate(
{ $project : {
_id : 0 ,
title : 1 ,
author : 1
}}
);
From you're example, the first operation in the pipeline would be to exclude the _id and include the other attribs.
从你的例子来看,管道中的第一个操作是排除 _id 并包含其他属性。
回答by Michael Pratt
I'm not familiar with motor, but you ought to be able to delete the property from the results dict directly.
我不熟悉电机,但您应该能够直接从结果字典中删除该属性。
>>> produits = {u'ok': 1.0, u'result': [{u'_id': None, u'pup': [{u'avt': {u'fto': 'whatever'}}]}]}
>>> prod = produits['result']
>>> del prod[0]['_id']
>>> print prod
[{u'pup': [{u'avt': {u'fto': 'whatever'}}]}]
回答by Xavier Guihot
Starting Mongo 4.2
, the $unset
aggregation operator can be used as an alternative syntax for $project
when used to only drop fields:
从 开始Mongo 4.2
,$unset
聚合运算符可用作仅用于$project
删除字段时的替代语法:
// { _id: "1sd", pup: [{ avt: { fto: "whatever"} }] }
// { _id: "d3r", pup: [{ avt: { fto: "whatever else"} }] }
db.collection.aggregate({ $unset: ["_id"] })
// { pup: [{ avt: { fto: "whatever" } } ] }
// { pup: [{ avt: { fto: "whatever else" } } ] }
回答by Danielo515
This is not exaclty a mongoWay of doing it, but you can use this factory to generate an object that includes all but _id
这不是一种 mongoWay 的做法,但您可以使用此工厂生成一个包含除 _id 以外的所有对象的对象
/**
* Factory that returns a $project object that excludes the _id property https://docs.mongodb.com/v3.0/reference/operator/aggregation/project/
* @params {String} variable list of properties to be included
* @return {Object} $project object including all the properties but _id
*/
function includeFactory(/* properties */){
var included = { "_id": 0 };
Array.prototype.slice.call(arguments).forEach(function(include){
included[include] = true
})
return { "$project": included }
}
Then use it like this:
然后像这样使用它:
cities.aggregate(
{ "$group": { "_id": null, "max": { "$max": "$age" }, "min": { "$min": "$age" }, "average": { "$avg": "$age" }, "total": { "$sum": "$count" } } },
includeFactory('max','min','average','total')
)