我想从我的阵列中的病人那里检索信息,但是在MongoDB网站上找不到这样做的命令。有人可以告诉我正确的检索数据的命令,例如,我想找回病人的名字吗?
{
"_id" : ObjectId("57113238bde91693e9ff69e7"),
"docname" : "Arthur Hovsepyan",
"job_desc" : "Hepatologist",
"sex" : "male",
"jobtype" : "fulltime",
"office" : "room 448",
"email" : "arturchik@hotmail.com",
"phone_number" : 862124343,
"address" : "68 Peterburg street,waterford",
"hours" : 12,
"patients" : [
{
"name" : "Jenny Power",
"ward_no" : 1,
"sex" : "female",
"termdays" : 2,
"illness_type" : "minor",
"age" : 22,
"phone_number" : 877285221,
"address" : "63 Johnston street ,Waterford"
},
{
"name" : "Marie Peters",
"ward_no" : 2,
"sex" : "female",
"termdays" : 0,
"illness_type" : "minor",
"age" : 21,
"phone_number" : 862145992,
"address" : "99 Grange,Waterford"
},
{
"name" : "Philip John",
"ward_no" : 2,
"sex" : "male",
"termdays" : 10,
"illness_type" : "serious",
"age" : 31,
"phone_number" : 861125981,
"address" : "12 Monvoy Bridge,Waterford"
},
{
"name" : "Marta Peters",
"ward_no" : 3,
"sex" : "female",
"termd7ays" : 0,
"illness_type" : "minor",
"age" : 31,
"phone_number" : 862125981,
"address" : "100 Grange Manor,Waterford"
}
]
}
答案 0 :(得分:1)
可以使用.aggregate()
方法和$map
运算符完成此操作。
db.collection.aggregate([
{ "$project": {
"patients": {
"$map": { "input": "$patients", "as": "p", "in": "$$p.name" }
}
}}
])
返回:
{
"_id" : ObjectId("57113238bde91693e9ff69e7"),
"patients" : [ "Jenny Power", "Marie Peters", "Philip John", "Marta Peters" ]
}
您还可以使用.distinct
从集合中获取所有名称。
db.collection.distinct("patients.name")
返回:
[ "Jenny Power", "Marie Peters", "Marta Peters", "Philip John" ]