aggregation-framework

Limit MongoDB aggregation $lookup to only 1 match

。_饼干妹妹 提交于 2021-01-27 07:10:50
问题 There are two collections, users and reports . My goal is to make an aggregation that gets all users and for each user includes the amount of user's last report . Here is my current aggregation: db.users.aggregate([{ $lookup: { from: 'reports', localField: '_id', foreignField: 'userId', as: 'report', }, }, { $project: { 'lastReportAmount': { $let: { vars: { lastReport: {'$arrayElemAt': ['$report', 0]}, }, in: '$$lastReport.amount', }, }, 'id': '$_id', 'name': 1, 'firstLogin': 1, 'email': 1, }

how to count a $lookup fields in mongo db?

最后都变了- 提交于 2021-01-27 04:20:06
问题 I'm quite new in mongodb, now I need to count a $lookup field, is it possible? I had something like this: result = await company.aggregate([ { $lookup: { from: 'userFocus', localField: '_id', foreignField: 'value', as: 'focusUsers' } }, { $project:{ name: 1, focusUsers: {userId: 1} } } ]) and the result looks like this: [ {_id: 'xxxx', name: 'first company', focusUsers: [user1, user2, user3...]}, {_id: 'yyyy', name: 'second company', focusUsers: []}, {_id: 'zzzz', name: 'third company',

Convert multiple array into one array in MongoDB, also remove the None values from Arrays

大城市里の小女人 提交于 2021-01-21 11:32:45
问题 Mongo DB Array Structure: { "time" :[ [2018-12-18T20:16:28.800Z , 2018-12-18T20:16:28.800Z, 2016-11-14T10:16:32.700Z], [2016-11-14T10:16:32.700Z, 2017-09-17T11:16:54.500Z, 2018-12-18T20:16:28.800Z], [2017-09-17T11:16:54.500Z, 2018-12-18T20:16:28.800Z, 2016-11-14T10:16:32.700Z] ], "bar": [ [2,5,9,8], [6,3,7,None], [7,8,None] ] } Expected output with One array without None values: "time" :[ 2018-12-18T20:16:28.800Z, 2018-12-18T20:16:28.800Z, 2016-11-14T10:16:32.700Z, 2016-11-14T10:16:32.700Z,

Convert multiple array into one array in MongoDB, also remove the None values from Arrays

蹲街弑〆低调 提交于 2021-01-21 11:31:08
问题 Mongo DB Array Structure: { "time" :[ [2018-12-18T20:16:28.800Z , 2018-12-18T20:16:28.800Z, 2016-11-14T10:16:32.700Z], [2016-11-14T10:16:32.700Z, 2017-09-17T11:16:54.500Z, 2018-12-18T20:16:28.800Z], [2017-09-17T11:16:54.500Z, 2018-12-18T20:16:28.800Z, 2016-11-14T10:16:32.700Z] ], "bar": [ [2,5,9,8], [6,3,7,None], [7,8,None] ] } Expected output with One array without None values: "time" :[ 2018-12-18T20:16:28.800Z, 2018-12-18T20:16:28.800Z, 2016-11-14T10:16:32.700Z, 2016-11-14T10:16:32.700Z,

mongoose Date comparing without time and Group by createdAt and staffId with Weekly, monthly and Yearly total of staff count by aggregation?

妖精的绣舞 提交于 2021-01-20 10:53:17
问题 Might be question is look like duplicate but apologize for it. I want to aggregate the weekly, Monthly, Yearly result basis of createdAt property without time and staffId . Model is like below: { "_id" : ObjectId("5f351f3d9d90b1281c44c5dp"), "staffId" : 12345, "category" : "trend", "page_route" : "http://example.com/rer", "expireAt" : ISODate("2020-08-13T11:08:45.196Z"), "createdAt" : ISODate("2020-08-13T11:08:45.199Z"), "updatedAt" : ISODate("2020-08-13T11:08:45.199Z"), "__v" : 0 } { "_id" :

mongoose Date comparing without time and Group by createdAt and staffId with Weekly, monthly and Yearly total of staff count by aggregation?

拜拜、爱过 提交于 2021-01-20 10:49:52
问题 Might be question is look like duplicate but apologize for it. I want to aggregate the weekly, Monthly, Yearly result basis of createdAt property without time and staffId . Model is like below: { "_id" : ObjectId("5f351f3d9d90b1281c44c5dp"), "staffId" : 12345, "category" : "trend", "page_route" : "http://example.com/rer", "expireAt" : ISODate("2020-08-13T11:08:45.196Z"), "createdAt" : ISODate("2020-08-13T11:08:45.199Z"), "updatedAt" : ISODate("2020-08-13T11:08:45.199Z"), "__v" : 0 } { "_id" :

MongoDB aggregation $or with $elemMatch, $expr inside $lookup pipeline

橙三吉。 提交于 2021-01-05 11:50:20
问题 I have 2 collections : Users collection : { _id: ObjectId } Toto Collection : { _id: ObjectId fieldA: {_id: ObjectId} fieldB: [{_id: ObjectId}, {_id: ObjectId}] } And I'm trying to join the element of A collection which fieldA's id OR one of fieldB's id is matching with user's id with following aggregation : db.users.aggregate([ { $lookup: { from: "Toto", let: { "user_id": "$_id" }, as: "fcollection", pipeline: [ { $match: {$or:[ {"fieldB": {$elemMatch: {"_id": "$$user_id"}}}, {$expr: {$eq :[

MongoDB aggregation $or with $elemMatch, $expr inside $lookup pipeline

人盡茶涼 提交于 2021-01-05 11:49:25
问题 I have 2 collections : Users collection : { _id: ObjectId } Toto Collection : { _id: ObjectId fieldA: {_id: ObjectId} fieldB: [{_id: ObjectId}, {_id: ObjectId}] } And I'm trying to join the element of A collection which fieldA's id OR one of fieldB's id is matching with user's id with following aggregation : db.users.aggregate([ { $lookup: { from: "Toto", let: { "user_id": "$_id" }, as: "fcollection", pipeline: [ { $match: {$or:[ {"fieldB": {$elemMatch: {"_id": "$$user_id"}}}, {$expr: {$eq :[

MongoError: Expression $in takes exactly 2 arguments. 1 were passed in

邮差的信 提交于 2021-01-05 06:37:28
问题 below is my query: while executing below query getting mongoerror : Expression $in takes exactly 2 arguments. 1 were passed in. i am using $in Comparison operator { "$expr": { "$not": { "$eq":{ "$and": [ { "PrName": { "$in": [ "pname" ] } }, { "AccountId": { "$in": [ "34562", "88765", "87654", "12345" ] } } ] } } } } 回答1: When you use $expr , the operator expressions syntax changes a little: { $in: [ <expression>, <array expression> ] } { "$expr": { "$not": { "$and": [ { "$in": [ "$PrName", [

How to get the documents based on Date Filters(Week, Month and Custom Dates) in MongoDB?

狂风中的少年 提交于 2021-01-04 06:37:14
问题 I am trying to fetch all the documents based on date filters as below in MongoDB: This Week only Last Week only This Month only Last Month only Between Custom Dates. My collection: [ { _id: "123", status: "seen", userId: "589", createdAt: ISODate("2020-11-17T04:35:29.646Z"), }, { _id: "223", status: "seen", userId: "589", createdAt: ISODate("2020-11-17T04:35:29.646Z"), }, { _id: "474", status: "unseen", userId: "589", createdAt: ISODate("2020-11-10T04:35:29.646Z"), }, { _id: "875", status: