Return only matched sub-document elements within a nested array

So the query you have actually selects the “document” just like it should. But what you are looking for is to “filter the arrays” contained so that the elements returned only match the condition of the query. The real answer is of course that unless you are really saving a lot of bandwidth by filtering … Read more

How to filter array in subdocument with MongoDB [duplicate]

Using aggregate is the right approach, but you need to $unwind the list array before applying the $match so that you can filter individual elements and then use $group to put it back together: db.test.aggregate([ { $match: {_id: ObjectId(“512e28984815cbfcb21646a7”)}}, { $unwind: ‘$list’}, { $match: {‘list.a’: {$gt: 3}}}, { $group: {_id: ‘$_id’, list: {$push: ‘$list.a’}}} ]) … Read more

Mongodb Explain for Aggregation framework

Starting with MongoDB version 3.0, simply changing the order from collection.aggregate(…).explain() to collection.explain().aggregate(…) will give you the desired results (documentation here). For older versions >= 2.6, you will need to use the explain option for aggregation pipeline operations explain:true db.collection.aggregate([ { $project : { “Tags._id” : 1 }}, { $unwind : “$Tags” }, { $match: … Read more

mongodb count num of distinct values per field/key

MongoDB has a distinct command which returns an array of distinct values for a field; you can check the length of the array for a count. There is a shell db.collection.distinct() helper as well: > db.countries.distinct(‘country’); [ “Spain”, “England”, “France”, “Australia” ] > db.countries.distinct(‘country’).length 4 As noted in the MongoDB documentation: Results must not be … Read more

Include all existing fields and add new fields to document

In 4.2+, you can use the $set aggregation pipeline operator which is nothing other than an alias to $addFieldsadded in 3.4 The $addFields stage is equivalent to a $project stage that explicitly specifies all existing fields in the input documents and adds the new fields. db.collection.aggregate([ { “$addFields”: { “custom_field”: “$obj.obj_field1” } } ])

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)