Mongoose,$ setIsSubset之前的对象列表

时间:2016-12-15 21:17:42

标签: node.js mongodb mongoose mongodb-query aggregation-framework

现在我有一个这样的聚合

   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'只保留具有“高”重要性的元素。或者' medium'

如何过滤这些元素,因为$ 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")
}

0 个答案:

没有答案