I have a collection:

{ "_id" : ObjectId("5c7c804f14b4c026ba7fcaba"), "values" : [ { "active" : true, "id" : 30, }, { "active" : true, "id" : 33, }, { "active" : true, "id" : 34, }, { "active" : true, "id" : 35, }, { "active" : true, "id" : 36, } ] 

}

You need to get the result:

 { "_id" : ObjectId("5c7c804f14b4c026ba7fcaba"), "values" : [ { "active" : true, "id" : 30, }, { "active" : true, "id" : 34, }, { "active" : true, "id" : 35, }, { "active" : true, "id" : 36, } ] 

}

that is, do not display value.id = 33 as a result. Can this be done not through the aggregation ..aggregate (), but through find ()? Maybe some kind of javascript code with the condition can be screwed, which is not value.id = 33?

1 answer 1

Try this:

 db.test.find( { 'value.id': { $ne: 33 } } ) 
  • Although ... probably, such a request will return a document where id != 33 - Dmytryk
  • Yes, this option is not suitable. I found a solution through .aggregate () and $ filter Example: db.getCollection ("properties"). Aggregate ([{"$ match": {"values.id": {"$ in": [43]}}} , {"$ addFields": {"values": {"$ filter": {"input": "$ values", "as": "value", "cond": {"$ setIsSubset": [["$ $ value.id "], [43]]}}}}}]] But the problem is that the aggregation is not suitable for me, I use the Yii2 framework, and such a request returns a collection of objects to me, and I need the request itself to form the active provider . - ViMax Studio