aggregation-framework

Does mongoose / mongodb have access to object references in schema during aggregate?

纵然是瞬间 提交于 2019-12-23 22:25:48
问题 I'm working on a query that reads from 2 different obj referentes inside my mongo database. I will use a simple example of what im looking for. I have 3 schemas: User = new Schema({ places:[{type: Schema.Types.ObjectId, ref:'Place'}], shouts:[{type: Schema.Types.ObjectId, ref:'Shout'}] }); Place = new Schema({ name:String, description:String, }); Shout = new Schema({ content:String, }); My biggest question is if mongoose or mongodb has access to objectId references when executing the

Get elements of an array in date range

让人想犯罪 __ 提交于 2019-12-23 22:13:34
问题 This is an example document in my MongoDB collection: var user: [{ "_id" : ObjectId("5391b11a3c0a9ac01300006d"), "injurydata" : { "injuryinformation" : [ { "status" : "current", "updateddate" : ISODate("2014-06-06T12:16:20.306Z"), "updatedby" : "", "dateofinjury" : ISODate("2014-06-27T18:30:00.000Z"), "_id" : ObjectId("5391b11a3c0a9ac01300006f") } ] } }, { "_id" : ObjectId("5391b11a3c0a9ac01300006d"), "injurydata" : { "injuryinformation" : [ { "status" : "current", "updateddate" : ISODate(

MongoDB Collection Structure Performance

独自空忆成欢 提交于 2019-12-23 20:26:48
问题 I have a MongoDB database of semi-complex records and my reporting queries are struggling as the collection size increases. I want to make some reporting Views that are optimized for quick searching and aggregating. Here is an sample format: var record = { fieldOne:"", fieldTwo:"", fieldThree:"", //There is approx 30 fields at this level ArrayOne:[ {subItem1:""}, {subItem2:""} // There are usually about 10-15 items in this array ], ArrayTwo:[ {subItem1:""}, //ArrayTwo items reference ArrayOne

Using aggregate $lookup and $mergeObjects

江枫思渺然 提交于 2019-12-23 19:07:21
问题 I want to join collection. before, I used only lookup, so that I could get separated field that is joined. but I need to get result similar mysql join. I noticed there is $lookup and $mergeObjects for this action but not working well. user collection model. { "_id": ObjectId("xxxxxxx"), //this is default id from mongoDB "name": 'admin user', "email": 'admin@test.com', "password": 'xxxxxxxx', "roles": [ { "id": 0, "approved": true },{ "id": 2, "approved": true } ] },{ "_id": ObjectId("xxxxxxx"

Is there any way to use $filter and $sum in the same $project?

末鹿安然 提交于 2019-12-23 17:43:53
问题 Let's say I have a document in my aggregate pipeline that looks like this: { scores: [ {type: 'quiz', score: 75}, {type: 'quiz', score: 62}, {type: 'final', score: 34}, ] } I'm using $project to transform it, and I'd like to get the sum of the the quiz scores, is there a way I could somehow chain my $filter and $sum . I know that I could potentially use two $project s, but the way I currently have my pipeline set up would force me to continue reproject a ton of keys in my second project,

MongoDB: groupby subdocument and count + add total count

痞子三分冷 提交于 2019-12-23 17:42:02
问题 What I want to achieve Suppose having the following subdocument: { "id":1, "url":"mysite.com", "views": [ {"ip":"1.1.1.1","date":"01-01-2015"}, {"ip":"2.2.2.2","date":"01-01-2015"}, {"ip":"1.1.1.1","date":"01-01-2015"}, {"ip":"1.1.1.1","date":"01-01-2015"} ] } I want to count: how many IPs there are based on the "ip" value and also count the total of subdocuments in "views" if possible in the same query , to achieve the following result: [ { "_id":"2.2.2.2", "count":1 }, { "_id":"1.1.1.1",

how to show 0 for week when no record is matching that week in $week mongodb query

吃可爱长大的小学妹 提交于 2019-12-23 16:26:09
问题 My collection looks like below with details /* 1 createdAt:6/13/2018, 5:17:07 PM*/ { "_id" : ObjectId("5b21043b18f3bc7c0be3414c"), "Number" : 242, "State" : "2", "City" : "3", "Website" : "", "Contact_Person_Name" : "Ajithmullassery", "CreatedById" : "Admin", "UpdatedById" : "Admin", "IsActive" : true, "UpdatedOn" : ISODate("2018-06-13T17:17:07.313+05:30"), "CreatedOn" : ISODate("2018-06-13T17:17:07.313+05:30") }, /* 2 createdAt:6/13/2018, 6:45:42 PM*/ { "_id" : ObjectId(

Find all documents that share max(value) found in aggregate step

随声附和 提交于 2019-12-23 16:23:59
问题 I'm trying to find all documents that share the max(num_sold). I cant just sort descending and do a limit(1) because I'd miss the case where other docs also have the same max(num_sold). Given this over-simplified dataset: {"item":"Apple", "num_sold": 49} {"item":"Orange", "num_sold": 55} {"item":"Peach", "num_sold": 55} {"item":"Grape", "num_sold": 20} {"item":"Banana", "num_sold": 20} I want to get back {"item":"Orange", "num_sold": 55} {"item":"Peach", "num_sold": 55} Using SQL, this is a

mongoDB query for retrieving from nested array collection

被刻印的时光 ゝ 提交于 2019-12-23 13:28:33
问题 { "_id" : ObjectId("576155a6cd87b68f7e6e42c9"), "First_Name" : "ok", "Last_Name" : "jaao", "Email" : "xyz@gmail.com", "Sessions" : [ { "Last_Login" : "Wed, Jun 14, 2016 6:48 PM", "Class" : "fb", "ID" : "123" }, { "Last_Login" : "Wed, Jun 15, 2016 6:48 PM", "ID" : "111", "Class" : "fb" } ], "Count" : 2 }, { "_id" : ObjectId("576155ccf6d8979e7e77df27"), "First_Name" : "abc", "Last_Name" : "xyz", "Email" : "xyz@gmail.com", "Sessions" : [ { "Last_Login" : "Wed, Jun 15, 2016 6:49 PM", "Class" :

How do you access a specific array item in MongoDB projection aggregation?

耗尽温柔 提交于 2019-12-23 12:37:15
问题 I'm trying to create a projection within a MongoDB aggregation function, see the following: [ {$match : {"geo" : {$ne: null}}}, {$project : {"id" : "$id_str", lat: "$geo.coordinates.0", lon: "$geo.coordinates.1"}} ] However when I do this it does not pass over the array item it simply projects an empty array to the properties lat and lon. What is needed here? I have looked at the Documentation but cannot figure it out, and even tried a permutation of $unwind and $group but with no success.