Fill missing records in mongo aggregate - node.js

I have a collection request
{
_Id: '5b8c0f3204a10228b00a1745,
createdAt: '2018-09-07T17:18:40.759Z',
type: "demo" , //["demo","free-try","download",...]
}
And I have a query for fetching the daily number for a specific type.
Query
Model.aggregate([
{
$match: { $expr: { $and: filters } },
},
{
$project: {
day: { $substr: ["$createdAt", 0, 10] },
type: 1,
createdAt: 1,
},
},
{
$group: {
_id: {
day: "$day",
type: "$type",
},
total: { $sum: 1 },
},
},
{
$sort: { _id: 1 },
},
{
$project: {
_id: "$_id.day",
date: "$_id.day",
type: "$_id.type",
total: 1,
},
}
])
So I get these results :
[
{
"total": 1,
"_id": "2021-01-06",
"date": "2021-01-06",
"type": "print"
},
{
"total": 1,
"_id": "2021-01-13",
"date": "2021-01-13",
"type": "download"
},
{
"total": 1,
"_id": "2021-03-09",
"date": "2021-03-09",
"type": "test"
},
{
"total": 2,
"_id": "2021-03-29",
"date": "2021-03-29",
"type": "demo"
},
{
"total": 1,
"_id": "2021-04-20",
"date": "2021-04-20",
"type": "test"
},
{
"total": 1,
"_id": "2021-04-21",
"date": "2021-04-21",
"type": "download"
},
{
"total": 1,
"_id": "2021-04-21",
"date": "2021-04-21",
"type": "renew"
},
{
"total": 1,
"_id": "2021-04-22",
"date": "2021-04-22",
"type": "print"
},
{
"total": 2,
"_id": "2021-04-26",
"date": "2021-04-26",
"type": "renew"
},
{
"total": 1,
"_id": "2021-05-03",
"date": "2021-05-03",
"type": "test"
},
{
"total": 1,
"_id": "2021-05-05",
"date": "2021-05-05",
"type": "print"
},
{
"total": 1,
"_id": "2021-05-05",
"date": "2021-05-05",
"type": "test"
},
{
"total": 2,
"_id": "2021-05-31",
"date": "2021-05-31",
"type": "demo"
},
{
"total": 1,
"_id": "2021-06-03",
"date": "2021-06-03",
"type": "renew"
}
]
up to here, everything is fine, but when I need to fill the missing record, so for example if in '2021-06-03' I don't have any request of type "demo" I need to insert this object with a total of 0
{
"total": 0,
"_id": "2021-05-31",
"date": "2021-05-31",
"type": "demo"
}
so I add this pipeline based on a solution proposed in here
Model.aggregate([
{
$match: { $expr: { $and: filters } },
},
{
$project: {
day: { $substr: ["$createdAt", 0, 10] },
type: 1,
createdAt: 1,
},
},
{
$group: {
_id: {
day: "$day",
type: "$type",
},
total: { $sum: 1 },
},
},
{
$sort: { _id: 1 },
},
{
$project: {
_id: "$_id.day",
date: "$_id.day",
type: "$_id.type",
total: 1,
},
},
{
$group: {
_id: null,
stats: { $push: "$$ROOT" },
},
},
{
$project: {
stats: {
$map: {
input: ["2018-09-01", "2018-09-02", "2018-09-03", "2018-09-04", "2018-09-05", "2018-09-06"],
as: "date",
in: {
$let: {
vars: { dateIndex: { $indexOfArray: ["$stats._id", "$$date"] } },
in: {
$cond: {
if: { $ne: ["$$dateIndex", -1] },
then: { $arrayElemAt: ["$stats", "$$dateIndex"] },
else: { _id: "$$date", date: "$$date", total: 0,type: "download" },
},
},
},
},
},
},
},
},
{
$unwind: "$stats",
},
{
$replaceRoot: {
newRoot: "$stats",
},
},
])
but this solution adds only a single object by missing day, and I need an object per type, so any solution would be appreciated

You can simply do it with $facet
$facet helps to categorize the incoming data. So I get two arrays. One is match dates and another one is non match dates. In the match dates we need to add the condition
$concatArrays to join multiple arrays into one
$unwind to deconstruct the array
$replaceRoot to make it to root
Here is the code
db.collection.aggregate([
{
"$facet": {
"matchDate": [
{
$match: {
date: { $in: [ "2021-01-13","2021-04-21" ] }
}
},
{
$addFields: {
total: { $cond: [{ $eq: [ "$type", "demo" ]}, 0, "$total" ] }
}
}
],
"nonMatchDate": [
{
$match: {
date: { $nin: [ "2021-01-13", "2021-04-21" ] }
}
}
]
}
},
{
$project: {
combined: {
"$concatArrays": [ "$matchDate", "$nonMatchDate" ]
}
}
},
{ "$unwind": "$combined" },
{ "$replaceRoot": { "newRoot": "$combined" }}
])
Working Mongo playground

Related

Aggregation: Return documents based on fields in a subdocument

I’m using an aggregation to return data via a lookup to build the links between documents.
At the moment, the linking is working when User A creates links between their own assets to navigate.
But if User A is viewing an asset that’s been shared with them by User B and navigates to one that has a link to an asset that hasn’t been shared with them, those are the documents I need to exclude from the results.
So, I need the documents for assets that have a document in attributes that contains my userId, or — as in the $match — the $_id of an attribute that's in the attributes array in assets. When an asset is shared with someone, a document in attributes is created.
The data for a Link is:
{
"_id": {
"$oid": "63769c377615fe4cdb4995a6"
},
"userId": "620920aa9ddac2074a50472f",
"toAsset": {
"$oid": "63769c117615fe4cdb499515"
},
"fromAsset": {
"$oid": "63769c067615fe4cdb4994d9"
},
"comment": "<p>Linking of Note 0001 to Note 0002.</p>",
"createdAt": {
"$date": {
"$numberLong": "1668717623761"
}
},
"updatedAt": {
"$date": {
"$numberLong": "1668717623761"
}
},
"isEmbedded": false,
"isActive": true,
"__v": 0
}
The data for an Asset, as in toAsset and fromAsset, is:
{
"_id": {
"$oid": "6377a8d834671794449f0dca"
},
"userId": "636b73f31527830f7bd7a47e",
"folderId": "636b73f31527830f7bd7a482",
"title": "Note that hasn't been shared",
"note": "<p>Here's a Note that hasn't been shared.</p>",
"typeOfAsset": "note",
"createdAt": {
"$date": {
"$numberLong": "1668786392389"
}
},
"updatedAt": {
"$date": {
"$numberLong": "1668786392389"
}
},
"isActive": 3,
"meta": [...],
"preferences": [...],
"sequence": 1,
"tags": [],
"attributes": [
{
"$oid": "6377a8d834671794449f0dc8"
}
],
"__v": 0
}
I’m using attributes to manage what assets have been shared with whom, and the data is:
{
"_id": {
"$oid": "6377a8d834671794449f0dc8"
},
"userId": "636b73f31527830f7bd7a47e",
"numberOfViews": 2,
"isFavourite": false,
"isToRead": false,
"typeOfAccess": "isOwner",
"sharing": {
"typeOfShare": "withUsers",
"sharedWith": [],
"segementsForUrl": []
},
"__v": 0
}
Now, the task here is to somehow how return the assets that have been shared, but after a bunch of different attempts (as per the code that’s been commented out), I’ve so far failed.
The code is:
const match = {
$match: {
[args.directionOfLink]: new mongoose.Types.ObjectId(args.assetId)
}
}
const project = {
$project: {
_id: 0,
id: '$_id',
userId: 1,
[directionOfLink]: 1,
comment: 1,
createdAt: 1,
updatedAt: 1,
isActive: 1,
score: {
$meta: 'searchScore'
}
}
}
const lookup = {
$lookup: {
from: 'assets',
localField: directionOfLink,
foreignField: '_id',
let: { attributesInAsset: '$attributes' },
pipeline: [
{
$lookup: {
from: 'attributes',
as: 'attributes',
pipeline: [{
$match: {
$expr: {
$in: [ '$_id', '$$attributesInAsset' ]
// $and: [
// { $eq: [ '$userId', context.body.variables.userId ] },
// { $in: [ '$typeOfAccess', ['isOwner', 'asAuthor', 'asReader'] ] },
// ]
}
}
}]
}
},
{
$project: {
_id: 1,
userId: 1,
folderId: 1,
title: 1,
typeOfAsset: 1,
attributes: 1,
createdAt: 1,
updatedAt: 1,
isActive: 1
}
}
],
as: directionOfLink
}
}
Here, directionOfLink is either "toAsset" or "fromAsset".
Any thoughts would be appreciated.
As a non expert in MongoDB, it's possible this isn't the most performant approach, but at least it works:
const lookup = {
$lookup: {
from: 'assets',
localField: directionOfLink,
foreignField: '_id',
as: directionOfLink,
pipeline: [
{
$lookup: {
from: 'assets_attributes',
as: 'attributesInAssets',
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: [ '$userId', context.body.variables.userId ] },
{ $in: [ '$typeOfAccess', ['isOwner', 'asAuthor', 'asReader'] ] },
]
}
}
}
]
}
},
{
$unwind: '$attributesInAssets'
},
{
$match: {
$expr: {
$in: [ '$attributesInAssets._id', '$attributes' ]
}
}
},
{
$group: {
_id: '$_id',
userId: { $first: '$userId' },
folderId: { $first: '$folderId' },
title: { $first: '$title' },
typeOfAsset: { $first: '$typeOfAsset' },
createdAt: { $first: '$createdAt' },
updatedAt: { $first: '$updatedAt' },
isActive: { $first: '$isActive' },
attributes: { $first: '$attributes' },
attributesInAssets: {
$push: '$attributesInAssets._id'
}
}
},
{
$project: {
_id: 1,
userId: 1,
folderId: 1,
title: 1,
typeOfAsset: 1,
attributes: 1,
attributesInAssets: 1,
createdAt: 1,
updatedAt: 1,
isActive: 1
}
}
]
}
}
const redact = {
$redact: {
$cond: {
if: {
$gt: [ {
$size: `$${directionOfLink}`
}, 0 ]
},
then: '$$KEEP',
else: '$$PRUNE'
}
}
}

Aggregate total and unique counts based on value type and unique visitorId - MongoDB

Similar to another question I had (Here). But now I'm trying to count unique and total events on daily basis for each event type, based on the following data shape:
{
username: "jack",
events: [
{
eventType: "party",
createdAt: "2022-01-23T10:26:11.214Z",
visitorInfo: {
visitorId: "87654321-0ebb-4238-8bf7-87654321"
}
},
{
eventType: "party",
createdAt: "2022-01-23T10:26:11.214Z",
visitorInfo: {
visitorId: "87654321-0ebb-4238-8bf7-87654321"
}
},
{
eventType: "party",
createdAt: "2022-01-23T10:26:11.214Z",
visitorInfo: {
visitorId: "01234567-0ebb-4238-8bf7-01234567"
}
},
{
eventType: "meeting",
createdAt: "2022-01-23T12:26:11.214Z",
visitorInfo: {
visitorId: "87654321-0ebb-4238-8bf7-87654321"
}
},
{
eventType: "meeting",
createdAt: "2022-01-23T11:26:11.214Z",
visitorInfo: {
visitorId: "87654321-0ebb-4238-8bf7-87654321"
}
},
{
eventType: "meeting",
createdAt: "2022-01-23T12:26:11.214Z",
visitorInfo: {
visitorId: "01234567-0ebb-4238-8bf7-01234567"
}
},
{
eventType: "party",
createdAt: "2022-01-30T10:26:11.214Z",
visitorInfo: {
visitorId: "12345678-0ebb-4238-8bf7-12345678"
}
},
{
eventType: "party",
createdAt: "2022-01-30T10:16:11.214Z",
visitorInfo: {
visitorId: "12345678-0ebb-4238-8bf7-12345678"
}
},
{
eventType: "meeting",
createdAt: "2022-01-30T12:36:11.224Z",
visitorInfo: {
visitorId: "12345678-0ebb-4238-8bf7-12345678"
}
},
{
eventType: "meeting",
createdAt: "2022-01-30T11:46:11.314Z",
visitorInfo: {
visitorId: "12345678-0ebb-4238-8bf7-12345678"
}
}
]
}
I'm trying to count events (all and unique ones based on visitorId) on date (daily).
This is what I have so far (thanks to #R2D2's guide on the approach):
Event.aggregate([
{ $match: { username: 'jack' } },
{ $unwind: "$events" },
{
$project: {
totalPartyEvents: {
$cond: [
{
$eq: ["$events.eventType", "party"],
},
1,
0,
],
},
uniquePartyEvents: { // where I'm stuck. I need to count unique events based on visitorId on current date for 'party' event type.
$cond: [
{
$eq: ["$events.eventType", "party"],
},
1,
0,
],
},
totalMeetingEvents: {
$cond: [
{
$eq: ["$events.eventType", "meeting"],
},
1,
0,
],
},
uniqueMeetingEvents: { // do the same for other events. maybe there's a better way to combine these (with facets).
$cond: [
{
$eq: ["$events.eventType", "meeting"],
},
1,
0,
],
},
date: "$events.createdAt",
},
},
{
$group: {
_id: {
$dateToString: { format: "%Y-%m-%d", date: "$date" },
},
totalPartyEvents: {
$sum: "$totalMeetingEvents",
},
uniquePartyEvents: {
$sum: "$totalMeetingEvents",
},
totalMeetingEvents: {
$sum: "$totalMeetingEvents",
},
uniqueMeetingEvents: {
$sum: "$uniqueMeetingEvents",
},
},
},
{
$project: {
date: "$_id",
uniquePartyEvents: 1,
totalPartyEvents: 1,
totalMeetingEvents:1,
uniqueMeetingEvents: 1,
},
},
{
$group: {
_id: "0",
dateAndEventFrequency: {
$push: "$$ROOT",
},
},
},
{
$project: {
_id: 0,
dateAndEventFrequency: 1,
},
},
]);
I tried using $addToSet but it's not used with $project (it works with $group).
Any new approach is welcome based on the data shape and the desired result I'm expecting. I used $project because I was already using it.
Basically what I'm hoping to get in the end:
dateAndEventFrequency: [
{
_id: "2022-01-23",
totalPartyEvents: 3,
uniquePartyEvents: 2,
totalMeetingEvents: 3,
uniqueMeetingEvents: 2,
date: "2022-01-23",
},
{
_id: "2022-01-30",
totalPartyEvents: 2,
uniquePartyEvents: 1,
totalMeetingEvents: 2,
uniqueMeetingEvents: 1,
date: "2022-01-30",
},
]
I'm using Mongoose and Nodejs. Any help or guidance is appreciated. Thanks!
mongo playground
db.collection.aggregate([
{
$match: {
username: "jack"
}
},
{
"$unwind": "$events"
},
{
"$match": {
"events.eventType": {
"$in": [
"meeting",
"party"
]
}
}
},
{
"$group": {
"_id": {
date: {
"$dateToString": {
format: "%Y-%m-%d",
date: "$events.createdAt"
}
},
"visitorId": "$events.visitorInfo.visitorId",
"eventType": "$events.eventType"
},
"count": {
"$sum": 1
}
}
},
{
"$group": {
"_id": {
"date": "$_id.date",
"eventType": "$_id.eventType"
},
"uniqueTotal": {
"$sum": 1
},
total: {
"$sum": "$count"
}
}
},
{
"$group": {
"_id": "$_id.date",
"partyUniqueTotal": {
"$sum": {
"$cond": [
{
$eq: [
"$_id.eventType",
"party"
],
},
"$uniqueTotal",
0
]
}
},
"totalPartyEvents": {
"$sum": {
"$cond": [
{
$eq: [
"$_id.eventType",
"party"
],
},
"$total",
0
]
}
},
"meetingUniqueTotal": {
"$sum": {
"$cond": [
{
$eq: [
"$_id.eventType",
"meeting"
],
},
"$uniqueTotal",
0
]
}
},
"totalmeetingEvents": {
"$sum": {
"$cond": [
{
$eq: [
"$_id.eventType",
"meeting"
],
},
"$total",
0
]
}
}
}
}
])

MongoDB aggregation : Group by Category and sum up the amount

I have the following structure in my collection (you don't have to mind the status) :
{
"_id": {
"$oid": "5e6355e71b14ee00175698cb"
},
"finance": {
"expenditure": [
{
"status": true,
"_id": { "$oid": "5e63562d1b14ee00175698df" },
"amount": { "$numberInt": "100" },
"category": "Sport"
},
{
"status": true,
"_id": { "$oid": "5e6356491b14ee00175698e0" },
"amount": { "$numberInt": "200" },
"category": "Sport"
},
{
"status": true,
"_id": { "$oid": "5e63565b1b14ee00175698e1" },
"amount": { "$numberInt": "50" },
"category": "Outdoor"
},
{
"status": true,
"_id": { "$oid": "5e63566d1b14ee00175698e2" },
"amount": { "$numberInt": "400" },
"category": "Outdoor"
}
]
}
}
My previos command was this:
User.aggregate([
{ $match: {_id: req.user._id} },
{ $unwind: '$finance.expenditure' },
{ $match: {'finance.expenditure.status': true} },
{ $sort: {'finance.expenditure.currentdate': -1} },
{
$group: {
_id: '$_id',
expenditure: { $push: '$finance.expenditure' }
}
}
])
With this I just get every single expenditure back.
But now I want to group the expenditures by their category and sum up the amount of every single expenditure for their group.
So it should look like this:
{ "amount": 300 }, "category": "Sport" },
{ "amount": 450 }, "category": "Outdoor" }
Thanks for your help
Instead of grouping on _id field group on category field & sum amount field:
db.collection.aggregate([
{ $match: {_id: req.user._id}},
{
$unwind: "$finance.expenditure"
},
{
$match: {
"finance.expenditure.status": true
}
},
{
$sort: {
"finance.expenditure.currentdate": -1
}
},
{
$group: {
_id: "$finance.expenditure.category",
amount: {
$sum: "$finance.expenditure.amount"
}
}
},
{
$project: {
_id: 0,
category: "$_id",
amount: 1
}
}
])
Test : MongoDB-Playground

How to get most repeated string array in Pymongo?

How can I get most repeated value for gender and age respectively?
My data:
[{ "_id": ObjectId("5dff27c0ac2d1547d87a1fe7"), "time": "2019-12-20 21:09:53",
"object": [{"Id": 1,"gender": "female","age": "0-10"},
{"Id": 2,"gender": "female","age": "20-30"}]
},
{ "_id": ObjectId("5dff27c0ac2d1547d87a1fe8"), "time": "2019-12-20 21:09:53",
"object": [{"Id": 1,"gender": "male","age": "0-10"},
{"Id": 2,"gender": "female","age": "30-40"}]
} ,
{ "_id": ObjectId("5dff27c0ac2d1547d87a1fe9"), "time": "2019-12-20 21:09:53",
"object": [{"Id": 1,"gender": "male","age": "10-15"},
{"Id": 2,"gender": "female","age": "30-40"},
{"Id": 3,"gender": "male","age": "0-10"}]
},
{ "_id": ObjectId("5dff27c0ac2d1547d87a1fea"), "time": "2019-12-20 21:09:53",
"object": [{"Id": 2,"gender": "male","age": "40-50"},
{"Id": 3,"gender": "male","age": "0-10"},
{"Id": 4,"gender": "male","age": "0-10"}]
}]
I have written below query,
mongo.db.xyz.aggregate([
{ "$unwind" : "$object"},
{"$group" : {"_id" : "$object.Id","_gen":{"$push":"$object.gender"},"_age":{"$push":"$object.age"}}},
{ "$project": { "_id" : "$_id", "gender":"$_gen","age":"$_age"}}
])
Below is the result I am getting,
[{"_id": 3,"age": ["0-10","0-10"],"gender": ["male","male"]},
{"_id": 2,"age": ["20-30","30-40","30-40","40-50"],"gender": ["female","female","female","male"]},
{"_id": 4,"age": ["0-10"],"gender": ["male"]},
{"_id": 1,"age": ["0-10","0-10","10-15"],"gender": ["female","male","male"]}
]
But I want the output to be ,
[{"_id": 3,"age": "0-10","gender": "male"},
{"_id": 2,"age": "30-40","gender": "female"},
{"_id": 4,"age": "0-10","gender": "male"},
{"_id": 1,"age": "0-10","gender": "male"}
]
Thinking about this problem I realized it was not so simple to get the mode of some fields independently in an array of objects with just one db query. To solve that I created a query to do it generically, based on your sample data.
db.collection.aggregate([
{
$unwind: {
path: "$arr"
}
},
{
$project: {
arr: {
$objectToArray: "$arr"
}
}
},
{
$unwind: {
path: "$arr"
}
},
{
$group: {
_id: {
_id: "$_id",
k: "$arr.k",
v: "$arr.v"
},
count: {
$sum: 1
}
}
},
{
$sort: {
count: -1
}
},
{
$group: {
_id: {
_id: "$_id._id",
k: "$_id.k"
},
v: {
$first: "$_id.v"
},
count: {
$first: "$count"
}
}
},
{
$group: {
_id: {
_id: "$_id._id"
},
arr: {
$push: {
k: "$_id.k",
v: {
mode: "$v",
count: "$count"
}
}
}
}
},
{
$project: {
arr: {
$arrayToObject: "$arr"
}
}
}
])
The secret to do that is to use the $objectToArray and the $arrayToObject operations, along with the $unwind and the correct $group stages. If you need a more detailed response on any stage please ask in the comments.
The output of the sample data is:
[
{
"_id": {
"_id": ObjectId("5dff27c0ac2d1547d87a1fea")
},
"arr": {
"Id": {
"count": 1,
"mode": 3
},
"age": {
"count": 2,
"mode": "0-10"
},
"gender": {
"count": 3,
"mode": "male"
}
}
},
{
"_id": {
"_id": ObjectId("5dff27c0ac2d1547d87a1fe7")
},
"arr": {
"Id": {
"count": 1,
"mode": 2
},
"age": {
"count": 1,
"mode": "0-10"
},
"gender": {
"count": 2,
"mode": "female"
}
}
},
{
"_id": {
"_id": ObjectId("5dff27c0ac2d1547d87a1fe9")
},
"arr": {
"Id": {
"count": 1,
"mode": 2
},
"age": {
"count": 1,
"mode": "30-40"
},
"gender": {
"count": 2,
"mode": "male"
}
}
},
{
"_id": {
"_id": ObjectId("5dff27c0ac2d1547d87a1fe8")
},
"arr": {
"Id": {
"count": 1,
"mode": 2
},
"age": {
"count": 1,
"mode": "30-40"
},
"gender": {
"count": 1,
"mode": "female"
}
}
}
]
After running it for a collection that has an array of objects named "arr" (edit the query if in your collection it has a different name) it will return the mode value and the number of occurrences of that value for each field. Objects that has that field unset will be not considered, but the ones with "null" will.

add key to nested array with condition

I have a simple datastructure in mongodb:
{
_id: ObjectID,
name: 'Name',
birthday: '25.05.2001'
items: [
{
_id: ObjectID,
name: 'ItemName',
info: 'ItemInfo',
},
{
_id: ObjectID,
name: 'ItemName',
info: 'ItemInfo',
}
]
}
Now i want a query, that takes a ObjectID (_id) of an item as criteria and gives me back the object with all items in the array AND projects a new field "selected" with value true or false into a field in the result of each array item:
I tried that with this query:
{ $unwind: '$items' },
{
$project: {
selected: {
$cond: { if: { 'items._id': itemObjectID }, then: true, else: false },
},
},
},
but MongoDB gives me back an error:
MongoError: FieldPath field names may not contain '.'.
Have no clue why its not working, any help or ideas? Thank you very much!
What you are missing here is $eq aggregation operator which checks the condition for the equality.
You can try below aggregation here if you want to check for ObjectId then you need to put mongoose.Types.ObjectId(_id)
db.collection.aggregate([
{ "$unwind": "$items" },
{ "$addFields": {
"items.selected": {
"$eq": [
1111,
"$items._id"
]
}
}},
{ "$group": {
"_id": "$_id",
"name": { "$first": "$name" },
"items": {
"$push": {
"_id": "$items._id",
"selected": "$items.selected"
}
}
}}
])
Will give following output
[
{
"_id": ObjectId("5a934e000102030405000000"),
"items": [
{
"_id": 1111,
"selected": true
},
{
"_id": 2222,
"selected": false
}
],
"name": "Name"
}
]
You can check it here
#Ashish: Thank you very much for your help! Your answer helped me to build the right query for me:
db.collection.aggregate([
{
$unwind: "$items"
},
{
$project: {
"items.name": 0,
"birthday": 0
}
},
{
"$addFields": {
"items.selected": {
"$eq": [
1111,
"$items._id"
]
}
}
},
{
$group: {
_id: "$_id",
"name": {
"$first": "$name"
},
items: {
$push: "$items"
}
}
},
{
$match: {
"items._id": {
$eq: 1111
}
}
},
])
and leads to a result that looks like:
[
{
"_id": ObjectId("5a934e000102030405000000"),
"items": [
{
"_id": 1111,
"selected": true
},
{
"_id": 2222,
"selected": false
}
],
"name": "Name"
}
]

Resources