我的目标是在mongoDB中查询数组中数组的子集。
例如,在“整个数据集”(如下)中,我想获取所有personreID为“ 5c6e3c74b9f5ed0016b00577”的chorePerson记录
整个数据集
[
{
"_id": "5c7464a26b47a13470411031",
"affiliation": "liss_family",
"year": 2019,
"weekNumber": 9,
"chart": [
{
"chorePerson": [
{
"_id": "5c7464a26b47a13470411054",
"person": "emily",
"personID": "5c6e3c74b9f5ed0016b00577",
"chore": "Catbox",
"choreID": "5c6f12cf4b19711d1824b16f"
},
{
"_id": "5c7464a26b47a13470411053",
"person": "Joe_2512",
"personID": "5c6ed442c30c29281cd75425",
"chore": "Sweep",
"choreID": "5c6f168bf09a1e0a50164683"
},
{
"_id": "5c7464a26b47a13470411052",
"person": "Tom_2510",
"personID": "5c703e5c094c084af05d8432",
"chore": "Trash",
"choreID": "5c6f16b2f09a1e0a50164686"
},
{
"_id": "5c7464a26b47a13470411051",
"person": "M_2513",
"personID": "5c745b80a3b3cb3a449b9605",
"chore": "Dishes",
"choreID": "5c7063476ee2723a24707b9d"
}
],
"_id": "5c7464a26b47a13470411050",
"ordinal": 0
},
{
"chorePerson": [
{
"_id": "5c7464a26b47a1347041104f",
"person": "Joe_2512",
"personID": "5c6ed442c30c29281cd75425",
"chore": "Catbox",
"choreID": "5c6f12cf4b19711d1824b16f"
},
{
"_id": "5c7464a26b47a1347041104e",
"person": "Tom_2510",
"personID": "5c703e5c094c084af05d8432",
"chore": "Sweep",
"choreID": "5c6f168bf09a1e0a50164683"
},
{
"_id": "5c7464a26b47a1347041104d",
"person": "M_2513",
"personID": "5c745b80a3b3cb3a449b9605",
"chore": "Trash",
"choreID": "5c6f16b2f09a1e0a50164686"
},
{
"_id": "5c7464a26b47a1347041104c",
"person": "emily",
"personID": "5c6e3c74b9f5ed0016b00577",
"chore": "Dishes",
"choreID": "5c7063476ee2723a24707b9d"
}
],
"_id": "5c7464a26b47a1347041104b",
"ordinal": 1
},
{
"chorePerson": [
{
"_id": "5c7464a26b47a1347041104a",
"person": "Tom_2510",
"personID": "5c703e5c094c084af05d8432",
"chore": "Catbox",
"choreID": "5c6f12cf4b19711d1824b16f"
},
{
"_id": "5c7464a26b47a13470411049",
"person": "M_2513",
"personID": "5c745b80a3b3cb3a449b9605",
"chore": "Sweep",
"choreID": "5c6f168bf09a1e0a50164683"
},
{
"_id": "5c7464a26b47a13470411048",
"person": "emily",
"personID": "5c6e3c74b9f5ed0016b00577",
"chore": "Trash",
"choreID": "5c6f16b2f09a1e0a50164686"
},
{
"_id": "5c7464a26b47a13470411047",
"person": "Joe_2512",
"personID": "5c6ed442c30c29281cd75425",
"chore": "Dishes",
"choreID": "5c7063476ee2723a24707b9d"
}
],
"_id": "5c7464a26b47a13470411046",
"ordinal": 2
},
{
"chorePerson": [
{
"_id": "5c7464a26b47a13470411045",
"person": "M_2513",
"personID": "5c745b80a3b3cb3a449b9605",
"chore": "Catbox",
"choreID": "5c6f12cf4b19711d1824b16f"
},
{
"_id": "5c7464a26b47a13470411044",
"person": "emily",
"personID": "5c6e3c74b9f5ed0016b00577",
"chore": "Sweep",
"choreID": "5c6f168bf09a1e0a50164683"
},
{
"_id": "5c7464a26b47a13470411043",
"person": "Joe_2512",
"personID": "5c6ed442c30c29281cd75425",
"chore": "Trash",
"choreID": "5c6f16b2f09a1e0a50164686"
},
{
"_id": "5c7464a26b47a13470411042",
"person": "Tom_2510",
"personID": "5c703e5c094c084af05d8432",
"chore": "Dishes",
"choreID": "5c7063476ee2723a24707b9d"
}
],
"_id": "5c7464a26b47a13470411041",
"ordinal": 3
},
{
"chorePerson": [
{
"_id": "5c7464a26b47a13470411040",
"person": "emily",
"personID": "5c6e3c74b9f5ed0016b00577",
"chore": "Catbox",
"choreID": "5c6f12cf4b19711d1824b16f"
},
{
"_id": "5c7464a26b47a1347041103f",
"person": "Joe_2512",
"personID": "5c6ed442c30c29281cd75425",
"chore": "Sweep",
"choreID": "5c6f168bf09a1e0a50164683"
},
{
"_id": "5c7464a26b47a1347041103e",
"person": "Tom_2510",
"personID": "5c703e5c094c084af05d8432",
"chore": "Trash",
"choreID": "5c6f16b2f09a1e0a50164686"
},
{
"_id": "5c7464a26b47a1347041103d",
"person": "M_2513",
"personID": "5c745b80a3b3cb3a449b9605",
"chore": "Dishes",
"choreID": "5c7063476ee2723a24707b9d"
}
],
"_id": "5c7464a26b47a1347041103c",
"ordinal": 4
},
{
"chorePerson": [
{
"_id": "5c7464a26b47a1347041103b",
"person": "Joe_2512",
"personID": "5c6ed442c30c29281cd75425",
"chore": "Catbox",
"choreID": "5c6f12cf4b19711d1824b16f"
},
{
"_id": "5c7464a26b47a1347041103a",
"person": "Tom_2510",
"personID": "5c703e5c094c084af05d8432",
"chore": "Sweep",
"choreID": "5c6f168bf09a1e0a50164683"
},
{
"_id": "5c7464a26b47a13470411039",
"person": "M_2513",
"personID": "5c745b80a3b3cb3a449b9605",
"chore": "Trash",
"choreID": "5c6f16b2f09a1e0a50164686"
},
{
"_id": "5c7464a26b47a13470411038",
"person": "emily",
"personID": "5c6e3c74b9f5ed0016b00577",
"chore": "Dishes",
"choreID": "5c7063476ee2723a24707b9d"
}
],
"_id": "5c7464a26b47a13470411037",
"ordinal": 5
},
{
"chorePerson": [
{
"_id": "5c7464a26b47a13470411036",
"person": "Tom_2510",
"personID": "5c703e5c094c084af05d8432",
"chore": "Catbox",
"choreID": "5c6f12cf4b19711d1824b16f"
},
{
"_id": "5c7464a26b47a13470411035",
"person": "M_2513",
"personID": "5c745b80a3b3cb3a449b9605",
"chore": "Sweep",
"choreID": "5c6f168bf09a1e0a50164683"
},
{
"_id": "5c7464a26b47a13470411034",
"person": "emily",
"personID": "5c6e3c74b9f5ed0016b00577",
"chore": "Trash",
"choreID": "5c6f16b2f09a1e0a50164686"
},
{
"_id": "5c7464a26b47a13470411033",
"person": "Joe_2512",
"personID": "5c6ed442c30c29281cd75425",
"chore": "Dishes",
"choreID": "5c7063476ee2723a24707b9d"
}
],
"_id": "5c7464a26b47a13470411032",
"ordinal": 6
}
],
"date": "2019-02-25T21:56:50.737Z",
"__v": 0
}
]
所需结果
所以这是我想要的结果。只有与“ 5c6e3c74b9f5ed0016b00577”的personID匹配的chorePerson结果:
[
{
"_id": "5c7464a26b47a13470411031",
"affiliation": "liss_family",
"year": 2019,
"weekNumber": 9,
"chart": [
{
"chorePerson": [
{
"_id": "5c7464a26b47a13470411054",
"person": "emily",
"personID": "5c6e3c74b9f5ed0016b00577",
"chore": "Catbox",
"choreID": "5c6f12cf4b19711d1824b16f"
}
],
"_id": "5c7464a26b47a13470411050",
"ordinal": 0
},
{
"chorePerson": [
{
"_id": "5c7464a26b47a1347041104c",
"person": "emily",
"personID": "5c6e3c74b9f5ed0016b00577",
"chore": "Dishes",
"choreID": "5c7063476ee2723a24707b9d"
}
],
"_id": "5c7464a26b47a1347041104b",
"ordinal": 1
},
{
"chorePerson": [
{
"_id": "5c7464a26b47a13470411048",
"person": "emily",
"personID": "5c6e3c74b9f5ed0016b00577",
"chore": "Trash",
"choreID": "5c6f16b2f09a1e0a50164686"
}
],
"_id": "5c7464a26b47a13470411046",
"ordinal": 2
},
{
"chorePerson": [
{
"_id": "5c7464a26b47a13470411044",
"person": "emily",
"personID": "5c6e3c74b9f5ed0016b00577",
"chore": "Sweep",
"choreID": "5c6f168bf09a1e0a50164683"
}
],
"_id": "5c7464a26b47a13470411041",
"ordinal": 3
},
{
"chorePerson": [
{
"_id": "5c7464a26b47a13470411040",
"person": "emily",
"personID": "5c6e3c74b9f5ed0016b00577",
"chore": "Catbox",
"choreID": "5c6f12cf4b19711d1824b16f"
}
],
"_id": "5c7464a26b47a1347041103c",
"ordinal": 4
},
{
"chorePerson": [
{
"_id": "5c7464a26b47a13470411038",
"person": "emily",
"personID": "5c6e3c74b9f5ed0016b00577",
"chore": "Dishes",
"choreID": "5c7063476ee2723a24707b9d"
}
],
"_id": "5c7464a26b47a13470411037",
"ordinal": 5
},
{
"chorePerson": [
{
"_id": "5c7464a26b47a13470411034",
"person": "emily",
"personID": "5c6e3c74b9f5ed0016b00577",
"chore": "Trash",
"choreID": "5c6f16b2f09a1e0a50164686"
}
],
"_id": "5c7464a26b47a13470411032",
"ordinal": 6
}
],
"date": "2019-02-25T21:56:50.737Z",
"__v": 0
}
]
我看了以下两篇文章,看来聚合可能是一种方法
https://www.devsbedevin.com/mongodb-find-findone-with-nested-array-filtering-finally/
How to use Aggregate in mongoose
背景
这是我的模式:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const chorePersonSchema = new mongoose.Schema({
person: {type: String, requried: true},
personID: {type: String, required: true},
chore: {type: String, required: true},
choreID: {type: String, required: true},
});
const chartSchema = new mongoose.Schema({
ordinal: {type: Number, required: true},
chorePerson : [{ type:chorePersonSchema }]
});
// create the schema
const ChoreChartSchema = new Schema({
affiliation: {type: String, required: true},
weekNumber: {type: Number, required: true},
year: {type: Number, required: true},
chart:[{type: chartSchema}],
date: {type: Date, default: Date.now},
})
module.exports = ChoreChart = mongoose.model('cm_chorechart', ChoreChartSchema)
尝试过
这是我尝试过的:
ChoreChart.aggregate([
{ $match : { affiliation: 'liss_family' } }
,{ $group: { "_id": { personID: '5c6e3c74b9f5ed0016b00577' } } }
]
)
.then(stuff =>{
return res.status(200).json(stuff);
})
.catch(err => {
return res.status(401).json(err);
})
以上返回:
[
{
"_id": {
"personID": "5c6e3c74b9f5ed0016b00577"
}
}
]
然后我尝试将组更改为以下内容:
,{ $group: { "_id": { "chorePerson.personID": '5c6e3c74b9f5ed0016b00577' } } }
返回哪个:
"errmsg": "FieldPath field names may not contain '.'.",
答案 0 :(得分:2)
您可以使用聚合
ChoreChart.aggregate([
{ "$match": { "affiliation": "liss_family" } },
{ "$addFields": {
"chart": {
"$map": {
"input": "$chart",
"as": "cc",
"in": {
"_id": "$$cc._id",
"ordinal": "$$cc.ordinal",
"chorePerson": {
"$filter": {
"input": "$$cc.chorePerson",
"as": "dd",
"cond": { "$eq": ["$$dd.personID", "5c6e3c74b9f5ed0016b00577"] }
}
}
}
}
}
}}
])