从 mongodb 文档中获取值作为列表
Fetch the values as List from the mongodb document
我有以下名为“empdept_rw”的文档,在这里我试图获取值作为特定键的所有可用文档的列表。
查询语句:
db.empdept_rw.find({"tremployeeid" : {"$in":db.employee_rw.distinct('tremployeeid')}}, {trDepartmentID : 1, _id :0})
当前输出:
{ "trDepartmentID" : "1234" }
{ "trDepartmentID" : "1235" }
预期输出:
["1234","1235"]
您可以在聚合管道中使用 $group
:
db.empdept_rw.aggregate([
{ $match: { tremployeeid: { $in: db.employee_rw.distinct('tremployeeid') } } },
{
$group: { _id: null, trDepartmentID: { $push: "$trDepartmentID" } }
}
]).toArray().shift().trDepartmentID
我有以下名为“empdept_rw”的文档,在这里我试图获取值作为特定键的所有可用文档的列表。 查询语句:
db.empdept_rw.find({"tremployeeid" : {"$in":db.employee_rw.distinct('tremployeeid')}}, {trDepartmentID : 1, _id :0})
当前输出:
{ "trDepartmentID" : "1234" }
{ "trDepartmentID" : "1235" }
预期输出:
["1234","1235"]
您可以在聚合管道中使用 $group
:
db.empdept_rw.aggregate([
{ $match: { tremployeeid: { $in: db.employee_rw.distinct('tremployeeid') } } },
{
$group: { _id: null, trDepartmentID: { $push: "$trDepartmentID" } }
}
]).toArray().shift().trDepartmentID