복붙노트

[MONGODB] 어떻게 배열에서 중복 된 항목을 제거하려면?

MONGODB

어떻게 배열에서 중복 된 항목을 제거하려면?

어떻게 배열에서 중복 된 항목을 제거하려면?

아래 예 "C ++에서 알고리즘"두번 첨가한다.

$ 해제 수정은 특정 필드를 제거하지만 어떻게 필드에서 항목을 제거하려면?

> db.users.find()

{ "_id" : ObjectId("4f6cd3c47156522f4f45b26f"), 
 "favorites" : { "books" : [ "Algorithms in C++",    
                            "The Art of Computer Programmning", 
                            "Graph Theory",      
                            "Algorithms in C++" ] }, 
  "name" : "robert" }

해결법

  1. ==============================

    1., ObjectId가 ( "4f6cd3c47156522f4f45b26f") : 당신이해야 할 일은 사용지도는 다음 { "_id"에 따라 전체 책을 대체하기 위해 $ 세트를 사용 .. 감지하고 중복 된 태그를 계산 줄일 수있다

    , ObjectId가 ( "4f6cd3c47156522f4f45b26f") : 당신이해야 할 일은 사용지도는 다음 { "_id"에 따라 전체 책을 대체하기 위해 $ 세트를 사용 .. 감지하고 중복 된 태그를 계산 줄일 수있다

    이 여기에 여러 번 논의되었습니다 .. 참조하시기 바랍니다

    맵리 듀스를 사용하여 중복 레코드 제거

    MongoDB의에서 인덱스 컬럼에 중복을 찾을 수있는 빠른 방법

    http://csanz.posterous.com/look-for-duplicates-using-mongodb-mapreduce

    http://www.mongodb.org/display/DOCS/MapReduce

    어떻게 맵리 듀스로 MongoDB를 중복 레코드를 제거하려면?

  2. ==============================

    2.MongoDB를 2.2 당신은 $의 풀림, $ 그룹과이를 달성하기 $ 프로젝트 단계로 통합 프레임 워크를 사용할 수 있습니다 :

    MongoDB를 2.2 당신은 $의 풀림, $ 그룹과이를 달성하기 $ 프로젝트 단계로 통합 프레임 워크를 사용할 수 있습니다 :

    db.users.aggregate([{$unwind: '$favorites.books'},
                        {$group: {_id: '$_id',
                                  books: {$addToSet: '$favorites.books'},
                                  name: {$first: '$name'}}},
                        {$project: {'favorites.books': '$books', name: '$name'}}
                       ])
    

    $ 그룹 집계 필드가 중첩 될 수 없기 때문에, 즐겨 찾기 필드의 이름을 바꿀 수있는 $ 프로젝트의 필요성을합니다.

  3. ==============================

    3.가장 쉬운 해결책은 사용 setUnion (몽고 2.6+)이다 :

    가장 쉬운 해결책은 사용 setUnion (몽고 2.6+)이다 :

    db.users.aggregate([
        {'$addFields': {'favorites.books': {'$setUnion': ['$favorites.books', []]}}}
    ])
    

    @ kynan의 대답에서 아이디어를 기반으로하지만, 명시 적으로 (몽고 3.4 이상)을 지정하지 않고 다른 모든 필드를 보존 또 다른 (더 긴) 버전 :

    > db.users.aggregate([
        {'$unwind': {
            'path': '$favorites.books',
            // output the document even if its list of books is empty
            'preserveNullAndEmptyArrays': true
        }},
        {'$group': {
            '_id': '$_id',
            'books': {'$addToSet': '$favorites.books'},
            // arbitrary name that doesn't exist on any document
            '_other_fields': {'$first': '$$ROOT'},
        }},
        {
          // the field, in the resulting document, has the value from the last document merged for the field. (c) docs
          // so the new deduped array value will be used
          '$replaceRoot': {'newRoot': {'$mergeObjects': ['$_other_fields', "$$ROOT"]}}
        },
        // this stage wouldn't be necessary if the field wasn't nested
        {'$addFields': {'favorites.books': '$books'}},
        {'$project': {'_other_fields': 0, 'books': 0}}
    ])
    
    { "_id" : ObjectId("4f6cd3c47156522f4f45b26f"), "name" : "robert", "favorites" : 
    { "books" : [ "The Art of Computer Programmning", "Graph Theory", "Algorithms in C++" ] } }    
    
  4. from https://stackoverflow.com/questions/9862255/how-to-remove-duplicate-entries-from-an-array by cc-by-sa and MIT license