MongoDB
 sql >> Baza danych >  >> NoSQL >> MongoDB

Wykonaj połączenie w mongoDB

Istnieje kilka podejść do tego, które możesz użyć aggregate metoda dla

db.collection.aggregate([
    // Assign an array of constants to each document
    { "$project": {
        "linkedIn": 1,
        "twitter": 1,
        "source": { "$cond": [1, ["linkedIn", "twitter"],0 ] }
    }},

    // Unwind the array
    { "$unwind": "$source" },

    // Conditionally push the fields based on the matching constant
    { "$group": { 
        "_id": "$_id",
        "data": { "$push": {
            "$cond": [
                { "$eq": [ "$source", "linkedIn" ] },
                { "source": "$source", "people": "$linkedIn.people" },
                { "source": "$source", "people": "$twitter.people" }
            ]
        }}
    }},

    // Unwind that array
    { "$unwind": "$data" },

    // Unwind the underlying people array
    { "$unwind": "$data.people" },

    // Project the required fields
    { "$project": {
        "_id": 0,
        "name": "$data.people.name",
        "source": "$data.source"
    }}
])

Lub z innym podejściem przy użyciu niektórych operatorów z MongoDB 2.6:

db.people.aggregate([
    // Unwind the "linkedIn" people
    { "$unwind": "$linkedIn.people" },

    // Tag their source and re-group the array
    { "$group": {
        "_id": "$_id",
        "linkedIn": { "$push": {
            "name": "$linkedIn.people.name",
            "source": { "$literal": "linkedIn" }
        }},
        "twitter": { "$first": "$twitter" }
    }},

    // Unwind the "twitter" people
    { "$unwind": "$twitter.people" },

    // Tag their source and re-group the array
    { "$group": {
        "_id": "$_id",
        "linkedIn": { "$first": "$linkedIn" },
        "twitter": { "$push": {
            "name":  "$twitter.people.name",
            "source": { "$literal": "twitter" }
        }}
    }},

    // Merge the sets with "$setUnion"
    { "$project": {
        "data": { "$setUnion": [ "$twitter", "$linkedIn" ] }
    }},

    // Unwind the union array
    { "$unwind": "$data" },

    // Project the fields
    { "$project": {
        "_id": 0,
        "name": "$data.name",
        "source": "$data.source"
    }}
])

I oczywiście, jeśli po prostu nie obchodziło Cię, jakie było źródło:

db.collection.aggregate([
    // Union the two arrays
    { "$project": {
        "data": { "$setUnion": [
            "$linkedIn.people",
            "$twitter.people"
        ]}
    }},

    // Unwind the union array
    { "$unwind": "$data" },

    // Project the fields
    { "$project": {
        "_id": 0,
        "name": "$data.name",
    }}

])


  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Jak utworzyć agregację z listy AggregationOperation w danych Spring MongoDB?

  2. Przykład danych wiosennych MongoDB nie działa

  3. Mongoose — zwiększanie wartości w tablicy obiektów

  4. Element tablicy aktualizacji MongoDB

  5. Błąd krytyczny PHP:nie znaleziono klasy „MongoDate”