私信  •  关注

Ashh

Ashh 最近创建的主题
Ashh 最近回复了
5 年前
回复了 Ashh 创建的主题 » Mongodb基于另一个数组元素过滤数组

您可以使用下面的聚合

db.collection.aggregate([
  { "$project": {
    "Quality": {
      "$map": {
        "input": { "$range": [0, { "$size": "$Quality" }] },
        "in": {
          "Quality": { "$arrayElemAt": ["$Quality", "$$this"] },
          "Pages": { "$arrayElemAt": ["$Pages", "$$this"] }
        }
      }
    }
  }},
  { "$project": {
    "newArrayField": {
      "$map": {
        "input": { "$setUnion": ["$Quality.Quality"] },
        "as": "m",
        "in": {
          "k": "$$m",
          "v": {
            "$filter": {
              "input": "$Quality",
              "as": "d",
              "cond": {
                "$eq": ["$$d.Quality", "$$m"]
              }
            }
          }
        }
      }
    }
  }},
  { "$project": {
    "d": {
      "$arrayToObject": {
        "$map": {
          "input": "$newArrayField",
          "in": {
            "k": "$$this.k",
            "v": { "$sum": "$$this.v.Pages" }
          }
        }
      }
    }
  }}
])

MongoPlayground

6 年前
回复了 Ashh 创建的主题 » MongoDB平均返回空值

您聚合的字段不正确。一定是

db.collection.aggregate([
  {
    "$group": {
      "_id": null,
      "Average": {
        "$avg": "$car.engineSize"
      }
    }
  }
])
6 年前
回复了 Ashh 创建的主题 » MongoDB不同结构统计嵌套文档数?

你可以用 $size

db.collection.aggregate([
  {
    "$project": {
      "totalJan": {
        "$size": "$2017.JAN"
      }
    }
  }
])

MongoPlayground

6 年前
回复了 Ashh 创建的主题 » mongodb,从$lookup中获取仅值列表

只是使用 .dot 用符号表示 name 领域

db.foo.aggregate([
  { "$lookup": {
    "from": "bar",
    "localField": "name",
    "foreignField": "foo",
    "as": "bars"
  }},
  { "$addFields": { "bars": "$bars.name" }}
])

MongoPlayground

5 年前
回复了 Ashh 创建的主题 » 获取MongoDB中字段最大值的文档

你可以把所有的田地 $max 像这样的接线员。。。它将为您提供max字段的文档,该字段最初用于 $最大值 反对。只是我用过 price 在这里。

db.getCollection("Collection").aggregate([
  { "$match": { "product": { "$in": ["product-a"] } } },
  { "$group": {
    "_id": null,
    "maxPrice": {
      "$max": {
        "price": "$price",
        "_id": "$_id"
      }
    }
  }}
])
5 年前
回复了 Ashh 创建的主题 » Mongodb排序问题

你可以用简单的 find 查询 sort 光标

db.traffic.find({}).sort({ "myarray.name": -1 })

docs

对于数组,小于比较或升序排序比较 数组的最小元素,大于比较或 降序排序比较数组中最大的元素。

6 年前
回复了 Ashh 创建的主题 » 删除MongoDB中嵌套数组中的特定对象

您可以使用以下查询

db.getCollection("test1").update(
  {
    "id": 1234,
    "posts": {
      "$elemMatch": {
        "$elemMatch": {
          "id": "0.1",
          "name": "jane",
          "message": "good morning"
        }
      }
    }
  },
  {
    "$pull": {
      "posts.$": {
        "id": "0.1",
        "name": "jane",
        "message": "good morning"
      }
    }
  }
)
6 年前
回复了 Ashh 创建的主题 » 仅当MongoDB中的外部字段不为空时查找

可以使用下面的聚合与MangoDB 三点六 及以上

Article.aggregate([
  { "$lookup": {
    "from": "comments",
    "let": { "articleId": "$_id" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": [ "$articleId", "$$articleId" ] } } },
      { "$lookup": {
        "from": "comments",
        "let": { "commentId": "$_id" },
        "pipeline": [
          { "$match": { "$expr": { "$eq": [ "$parentId", "$$commentId" ] } } }
        ],
        "as": "answers"
      }}
    ],
    "as": "comments"
  }}
])

主要的一点在上面的回答中没有提到,

我们需要传递带有选项的查询字符串 "useUnicode=yes" "characterEncoding=UTF-8" 连接字符串中

像这样的东西

mysql://USERNAME:PASSWORD@HOSTNAME:PORT/DATABASE_NAME?useUnicode=yes&characterEncoding=UTF-8
6 年前
回复了 Ashh 创建的主题 » 删除除第一个索引之外的MongoDB数组键

使用 $range 聚合以查找数组的索引 courses 然后使用 $map 聚合和删除 ("$$REMOVE") 索引不相等的字段 ($ne) 0

对于蒙哥大 三点六 及以上

db.collection.aggregate([
  { "$addFields": {
    "courses": {
      "$map": {
        "input": { "$range": [0, {"$size": "$courses" }] },
        "in": {
          "$let": {
          "vars": { "c": {"$arrayElemAt": ["$courses", "$$this"]} },
            "in": {
              "stateName": "$$c.stateName",
              "duration": "$$c.duration",
              "lag": "$$c.lag",
              "courseType": "$$c.courseType",
              "scheduledStartDate": { "$cond": [{ "$ne": ["$$this", 0] }, "$$REMOVE", "$$c.scheduledStartDate"] },
              "scheduledEndDate": "$$c.scheduledEndDate",
              "scheduledEndDate": { "$cond": [{ "$ne": ["$$this", 0] }, "$$REMOVE", "$$c.scheduledEndDate"] },
              "transitionType": "$$c.transitionType"
            }
          }
        }
      }
    }
  }}
])

对于MongoDB 三点六

db.collection.aggregate([
  { "$addFields": {
    "courses": {
      "$map": {
        "input": { "$range": [0, { "$size": "$courses" }] },
        "in": {
          "$let": {
            "vars": { "c": { "$arrayElemAt": ["$courses", "$$this"] }},
            "in": {
              "$cond": [
                { "$eq": ["$$this", 0] },
                {
                  "stateName": "$$c.stateName",
                  "duration": "$$c.duration",
                  "lag": "$$c.lag",
                  "courseType": "$$c.courseType",
                  "scheduledStartDate": "$$c.scheduledStartDate",
                  "scheduledEndDate": "$$c.scheduledEndDate",
                  "stateName": "$$c.stateName",
                  "transitionType": "$$c.transitionType"
                },
                {
                  "stateName": "$$c.stateName",
                  "duration": "$$c.duration",
                  "lag": "$$c.lag",
                  "courseType": "$$c.courseType",
                  "transitionType": "$$c.transitionType"
                }
              ]
            }
          }
        }
      }
    }
  }}
])

哪个将 return

[
  {
    "_id": "1234",
    "completionStatus": "F",
    "courses": [
      {
        "courseProgress": 100,
        "courseType": "3",
        "duration": "5",
        "lag": "2",
        "scheduledEndDate": ISODate("2018-12-01T08:31:30Z"),
        "scheduledStartDate": ISODate("2018-11-27T08:31:30Z"),
        "stateName": "Stage 1",
        "transitionType": "onAssignment"
      },
      {
        "courseType": "2",
        "duration": "60",
        "lag": "60",
        "stateName": "2nd stage",
        "transitionType": "onAssignment"
      },
      {
        "courseType": "1",
        "duration": "5",
        "lag": "2",
        "stateName": "3rd Stage",
        "transitionType": "onAssignment"
      },
      {
        "courseType": "1",
        "duration": 1,
        "lag": "10",
        "stateName": "4th stage",
        "transitionType": "onAssignment"
      },
      {
        "courseType": "3",
        "duration": "1",
        "lag": "0",
        "stateName": "5th",
        "transitionType": "onAssignment"
      },
      {
        "courseType": "3",
        "duration": "5",
        "lag": "5",
        "stateName": "6th",
        "transitionType": "onAssignment"
      },
      {
        "courseType": "3",
        "duration": 1,
        "lag": "9",
        "stateName": "7th ",
        "transitionType": "onAssignment"
      },
      {
        "courseType": "3",
        "duration": "66",
        "lag": 0,
        "stateName": "8th",
        "transitionType": "onAssignment"
      },
      {
        "courseType": "1",
        "duration": "61",
        "lag": 0,
        "stateName": "9th",
        "transitionType": "onAssignment"
      },
      {
        "courseType": "3",
        "duration": "80",
        "lag": 0,
        "stateName": "10th",
        "transitionType": "onAssignment"
      },
      {
        "courseType": "3",
        "duration": "8",
        "lag": 0,
        "stateName": "11th",
        "transitionType": "onAssignment"
      }
    ],
    "created": ISODate("2018-11-27T08:31:32.082Z"),
    "currentState": {
      "courseId": "116",
      "courseProgress": 100
    },
    "modified": ISODate("2018-11-27T08:31:32.082Z"),
    "userId": "23",
    "userStatus": 1
  }
]
6 年前
回复了 Ashh 创建的主题 » 如何有条件地连接MongoDB聚合中的字段

你可以在下面使用 $project 舞台使用 $cond 聚合运算符

{ "$project": {
  "field1": { "$cond": [{ "$ne": ["$field1", null] }, { "$concat": ["$field1", "-"] }, ""] },
  "field3": { "$cond": [{ "$eq": ["$field3", "ok"] }, "approved", "pending"] },
  "field2": 1
}},
{ "$project": {
  "fieldName": { "$concat": ["$field1", "$field2", "$field3"] }
}}

甚至在一个阶段

{ "$project": {
  "fieldName": {
    "$concat": [
      { "$cond": [{ "$ne": ["$field1", null] }, { "$concat": ["$field1", "-"] }, ""] },
      "$field2",
      { "$cond": [{ "$eq": ["$field3", "ok"] }, "approved", "pending"] }
    ]
  }
}}

你必须使用 $push 累加器

db.collection.aggregate([
  { "$group": {
    "_id": "$roomNo",
    "availability": {
      "$push": {
        "slotEnd": "$slotEnd",
        "slotStart": "$slotStart"
      }
    }
  }}
])
6 年前
回复了 Ashh 创建的主题 » 从array mongodb中查找前3个值

您可以使用下面的聚合

db.collection.aggregate([
  { "$unwind": "$users" },
  { "$sort": { "users.level": -1 }},
  { "$group": {
    "_id": "$_id",
    "users": { "$push": "$users" }
  }},
  { "$addFields": { "users": { "$slice": ["$users", 3] }}}
])

MongoPlayground