반응형
_id를 집계에서 숨기는 방법?
다음 질문이 있습니다.
produits = yield motor.Op(db.users.aggregate, [{"$unwind":"$pup"},{"$match":{"pup.spec.np":nomp}}, {"$group":{"_id":"$pup.spec.id","pup":{"$push":"$pup"}}}])
그 결과는 다음과 같습니다.
print produits
{u'ok': 1.0, u'result': [{u'_id': None, u'pup': [{u'avt': {u'fto': ..all the results}}]}]}
제가 할 수 있는 일은:
prod = produits["result"]
[{u'_id': None, u'pup': [{u'avt': {u'fto': ..all the results}}]}]
하지만 어떻게 숨을 수 있죠?"_id"내가 얻을 수 있는 건
[{u'pup': [{u'avt': {u'fto': ..all the results}}]}]
일반적인 질문에서 저는 단순히 다음과 같은 것을 추가할 것입니다.{"_id":0}하지만 여기서는 작동하지 않습니다.
mongodb 문서에서
$project 결과를 제외할 수 있습니다._id이것이 당신이 의미하는 것입니까?
http://docs.mongodb.org/manual/reference/aggregation/ #http://pipeline
참고 _id 필드는 항상 기본적으로 포함됩니다.다음과 같이 _id를 명시적으로 제외할 수 있습니다.
db.article.aggregate(
{ $project : {
_id : 0 ,
title : 1 ,
author : 1
}}
);
이 예에서 파이프라인의 첫 번째 작업은 _id를 제외하고 다른 속성을 포함하는 것입니다.
시작하는Mongo 4.2집계 연산자를 대체 구문으로 사용할 수 있습니다.$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" } } ] }
모터에 대해 잘 모르지만, 당신은 직접 결과 받아쓰기에서 속성을 삭제할 수 있어야 합니다.
>>> 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'}}]}]
이것은 정확한 방법은 아니지만 이 팩토리를 사용하여 _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 }
}
그런 다음 다음과 같이 사용합니다.
cities.aggregate(
{ "$group": { "_id": null, "max": { "$max": "$age" }, "min": { "$min": "$age" }, "average": { "$avg": "$age" }, "total": { "$sum": "$count" } } },
includeFactory('max','min','average','total')
)
언급URL : https://stackoverflow.com/questions/15776453/how-to-hide-id-from-aggregation
반응형
'programing' 카테고리의 다른 글
| 'urlencode'에는 'urlencode' 특성이 없습니다. (0) | 2023.05.12 |
|---|---|
| 파일에서 대소문자를 구분하지 않는 문자열을 grep하는 방법은 무엇입니까? (0) | 2023.05.12 |
| 큰 정수를 가능한 가장 작은 문자열로 압축 (0) | 2023.05.12 |
| Bash에서 여러 텍스트 파일을 단일 파일로 연결 (0) | 2023.05.12 |
| MongoDB가 서버를 시작할 수 없습니다.이 빌드의 mongod에서는 기본 스토리지 엔진 'wiredTiger'를 사용할 수 없습니다. (0) | 2023.05.12 |