【问题标题】:Fetch the values as List from the mongodb document从 mongodb 文档中获取值作为 List
【发布时间】:2021-07-05 12:22:38
【问题描述】:

我有以下名为“empdept_rw”的文档,在这里我试图获取值作为特定键的所有可用文档的列表。 查询语句:

db.empdept_rw.find({"tremployeeid" : {"$in":db.employee_rw.distinct('tremployeeid')}}, {trDepartmentID : 1, _id :0})

当前输出:

{ "trDepartmentID" : "1234" }
{ "trDepartmentID" : "1235" }

预期输出:

["1234","1235"]

【问题讨论】:

    标签: mongodb collections


    【解决方案1】:

    您可以在聚合管道中使用$group

    db.empdept_rw.aggregate([
       { $match: { tremployeeid: { $in: db.employee_rw.distinct('tremployeeid') } } },
       {
          $group: { _id: null, trDepartmentID: { $push: "$trDepartmentID" } }
       }
    ]).toArray().shift().trDepartmentID
    

    【讨论】:

      猜你喜欢
      • 2017-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-16
      • 2014-11-15
      • 2021-08-17
      • 2023-01-03
      • 1970-01-01
      相关资源
      最近更新 更多