Mongodb aggregation pipeline use

Time:2020-6-28

Data preparation

[
    {
        "name": {
            "first_name": "qingquan",
            "last_name": "zeng"
        },
        "balance": 100
    },
    {
        "name": {
            "first_name": "fengxia",
            "last_name": "yu"
        },
        "balance": 200
    }
]

insert data

db.accounts.insert([{"name": {"first_name": "qingquan","last_name": "zeng"},"balance": 100},{"name": {"first_name": "fengxia","last_name": "yu"},"balance": 200}])

Data query

$project

#In addition to the projection effect, the $project in aggregate directly uses a nonexistent field client_ Name, equivalent to as syntax in MySQL
> db.accounts.aggregate([{
... $project:{
... _id:0,
... balance:1,
... client_name:"$name.first_name"
... }
... }]);
{ "balance" : 100, "client_name" : "qingquan" }
{ "balance" : 200, "client_name" : "fengxia" }
#Because of middle_ Name does not exist, the result is null
> db.accounts.aggregate([{
... $project:{
... _id:0,
... balance:1,
... name_arr:["$name.first_name","$name.middle_name","$name.first_name"]
... }
... }]);
{ "balance" : 100, "name_arr" : [ "qingquan", null, "qingquan" ] }
{ "balance" : 200, "name_arr" : [ "fengxia", null, "fengxia" ] }

$matchThe document filter syntax used in is the same as that used when reading documents

db.accounts.aggregate([
    {
        $match: {
            "name.first_name": 'fengxia'
        }
    }
])
{ "_id" : ObjectId("5d80fd4471c6b2236fb80de9"), "name" : { "first_name" : "fengxia", "last_name" : "yu" }, "balance" : 200 }

take$project$match$skip$limitCombination

db.accounts.aggregate([
    {
        $match: {
            $or: [
                {
                    "name.first_name": 'fengxia'
                },
                {
                    "name.first_name": 'qingquan'
                },          
            ]
        }
    },
    {
        $project: {
            _id: 0
        }
    },
    {
        $skip: 1
    },
    {
        $limit: 1
    }
])
{ "name" : { "first_name" : "fengxia", "last_name" : "yu" }, "balance" : 200 }

$unwind
Modify the data in this section

db.accounts.update({'name.first_name':'qingquan'},{
 $set:{
  "currency":["CNY","USD"]
 }
})

db.accounts.update({'name.first_name':'fengxia'},{
 $set:{
  "currency":"GBP"
 }
})

The modified data is as follows: one user’s current is an array, and the other user’s current is a string

> db.accounts.find()
{ "_id" : ObjectId("5d80fd4471c6b2236fb80de8"), "name" : { "first_name" : "qingquan", "last_name" : "zeng" }, "balance" : 100, "currency" : [ "CNY", "USD" ] }
{ "_id" : ObjectId("5d80fd4471c6b2236fb80de9"), "name" : { "first_name" : "fengxia", "last_name" : "yu" }, "balance" : 200, "currency" : "GBP" }

By using unwind to tile array elements, you can split the current record into array records, from one record to multiple records

db.accounts.aggregate([
    {
        $unwind: {
            path: "$currency"
        }
    }
])
{ "_id" : ObjectId("5d80fd4471c6b2236fb80de8"), "name" : { "first_name" : "qingquan", "last_name" : "zeng" }, "balance" : 100, "currency" : "CNY" }
{ "_id" : ObjectId("5d80fd4471c6b2236fb80de8"), "name" : { "first_name" : "qingquan", "last_name" : "zeng" }, "balance" : 100, "currency" : "USD" }
{ "_id" : ObjectId("5d80fd4471c6b2236fb80de9"), "name" : { "first_name" : "fengxia", "last_name" : "yu" }, "balance" : 200, "currency" : "GBP" }

To facilitate troubleshooting, you can also set a field to mark the position of each element in the original array after expanding the array

db.accounts.aggregate([
    {
        $unwind: {
            path: "$currency",
            includeArrayIndex:"origin_index"
        }
    }
])
{ "_id" : ObjectId("5d80c37349f3060f1212a055"), "name" : { "first_name" : "qingquan", "last_name" : "zeng" }, "balance" : 100, "currency" : "CNY", "origin_index" : NumberLong(0) }
{ "_id" : ObjectId("5d80c37349f3060f1212a055"), "name" : { "first_name" : "qingquan", "last_name" : "zeng" }, "balance" : 100, "currency" : "USD", "origin_index" : NumberLong(1) }
{ "_id" : ObjectId("5d80c37349f3060f1212a056"), "name" : { "first_name" : "fengxia", "last_name" : "yu" }, "balance" : 200, "currency" : "GBP", "origin_index" : null }

One more thing to note is that,$unwindBefore generating results, the following records will be filtered directly by default:

  • The current field is an empty array
  • Current field does not exist
  • Current field is null

If you do not want to filter, you can set preservenullandemptyarries to true

db.accounts.aggregate([
    {
        $unwind: {
            path: "$currency",
            includeArrayIndex: "origin_index",
            preserveNullAndEmptyArrays: true
        }
    }
])

$sort

  • 1 from small to large
  • -1 from large to small
db.accounts.aggregate([
    {
        $sort: {
            balance: -1
        }
    }
])
{ "_id" : ObjectId("5d80c37349f3060f1212a056"), "name" : { "first_name" : "fengxia", "last_name" : "yu" }, "balance" : 200, "currency" : "GBP" }
{ "_id" : ObjectId("5d80c37349f3060f1212a055"), "name" : { "first_name" : "qingquan", "last_name" : "zeng" }, "balance" : 100, "currency" : [ "CNY", "USD" ] }

Mongodb aggregation duplicate problem https://jacoobwang.github.io/…