權之前對象的過濾器列表現在我有這樣的
ObjectWithElement.aggregate([{
$redact: {
'$cond': [
{
'$eq': [
{ $setIsSubset: ['$elements.element', myElements] }, true
]
},
'$$KEEP',
'$$PRUNE'
]
}
}
]).exec(listCallback);
元的方案是聚集:
var ElementSchema = new Schema({
name: {
type: String,
default: '',
required: 'Please fill name',
trim: true
},
importance: {
type: String,
default: 'Medium',
enum: ['Low', 'Medium', 'High']
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
},
{
timestamps: true
});
但我想過濾「$ elements.element '通過只保留具有「高」或「中」重要性的元素
如何過濾這些元素,因爲$ setIsSubset操作?
一些數據:
MyElements
{
"_id" : ObjectId("5834b78271dc8b8c24cec6aa"),
"user" : ObjectId("581108f40e10a7e846456737"),
"elements" : [
{
"element" : ObjectId("581b7c645057c4602f48627f"),
"quantity" : 1,
"unit" : ObjectId("581ca0da5b1e3058521a6d8b"),
"_id" : ObjectId("58540ddb431dc71048974e61")
}
],
"__v" : 9,
"createdAt" : ISODate("2016-11-22T21:24:18.000Z"),
"updatedAt" : ISODate("2016-12-16T15:52:59.841Z")
}
元素
{
"_id" : ObjectId("581b7c645057c4602f48627f"),
"user" : ObjectId("581108f40e10a7e846456737"),
"importance" : "High",
"name" : "Element1",
"__v" : 0,
"createdAt" : ISODate("2016-11-03T18:05:24.000Z"),
"updatedAt" : ISODate("2016-12-15T21:40:03.502Z")
},
{
"_id" : ObjectId("581ca0e75b1e3058521a6d8c"),
"user" : ObjectId("581108f40e10a7e846456737"),
"importance" : "Low",
"name" : "Element2",
"__v" : 0
"createdAt" : ISODate("2016-11-03T18:05:26.000Z"),
"updatedAt" : ISODate("2016-12-15T21:44:03.502Z")
}
ObjectWithElements
{
"_id" : ObjectId("582e1b627836bcc045c06325"),
"user" : ObjectId("581108f40e10a7e846456737"),
"name" : "Object1",
"elements" : [
{
"element" : ObjectId("581b7c645057c4602f48627f"),
"quantity" : 1,
"unit" : ObjectId("581ca0da5b1e3058521a6d8b"),
"_id" : ObjectId("582e1b627836bcc045c06328")
},
{
"element" : ObjectId("581ca0e75b1e3058521a6d8c"),
"quantity" : 1,
"unit" : ObjectId("581ca0da5b1e3058521a6d8b"),
"_id" : ObjectId("58530046cfc0c384010f0839")
}
],
"__v" : 2,
"createdAt" : ISODate("2016-11-17T21:04:34.000Z"),
"updatedAt" : ISODate("2016-12-15T20:42:46.843Z")
},
{
"_id" : ObjectId("582e2c05b4e900d03d494fef"),
"user" : ObjectId("581108f40e10a7e846456737"),
"name" : "Object2",
"elements" : [
{
"element" : ObjectId("581b7c645057c4602f48627f"),
"quantity" : 2,
"unit" : ObjectId("581ca0da5b1e3058521a6d8b"),
"_id" : ObjectId("582e2c05b4e900d03d494ff2")
}
],
"__v" : 2,
"createdAt" : ISODate("2016-11-17T22:15:33.000Z"),
"updatedAt" : ISODate("2016-12-15T20:42:59.999Z")
}
哪裏是在你的架構中定義的''$ elements.element''表達了'elements'場更換$ setIsSubset?我只能看到定義的5個字段,即'名稱','重要性','用戶','createdAt'和'updatedAt' ... – chridam
整個對象是一個元素 – Leamas