aggregation-framework

sort by embedded object value in Mongodb

二次信任 提交于 2019-12-25 03:07:04
问题 I have a schema like this: { tags:[ { id:"t1", score:70 }, { id:"t1", score:60 } ] } I want to sort the query on tag.id search to get sorted by the corresponding score. So if I search db.collection.find({tags.id:"t1"}).sort({tags.score:-1}) , it sorted by score of the "t1" object not other tags. Any suggestion? 回答1: If you need to calculate out something like this at runtime, with "filtered" content from the array determining the sort order, then you best do something with .aggregate() to

MongoDB vs MySQL Performance - Simple Query

寵の児 提交于 2019-12-25 02:03:27
问题 I am doing a comparison of mongodb with respect to mysql and imported the mysql data into the mongodb collection (>500000 records). the collection looks like this: { "_id" : ObjectId(""), "idSequence" : , "TestNumber" : , "TestName" : "", "S1" : , "S2" : , "Slottxt" : "", "DUT" : , "DUTtxt" : "", "DUTver" : "", "Voltage" : , "Temperature" : , "Rate" : , "ParamX" : "", "ParamY" : "", "Result" : , "TimeStart" : new Date(""), "TimeStop" : new Date(""), "Operator" : "", "ErrorNumber" : ,

Matching ObjectId to String for $graphLookup

北城以北 提交于 2019-12-25 01:45:11
问题 I'm trying to run a $graphLookup like demonstrated in print bellow: The objective is to, given a specific record (commented $match there), retrieve it's full "path" throught immediateAncestors property. As you can see, it's not happening. I introduced $convert here to deal with _id from collection as string , believing it could be possible to "match" with _id from immediateAncestors records list (which is a string ). So, I did run another test with different data (no ObjectId s involved): db

Mongo aggregation Convert string to date in version 3.4.7

懵懂的女人 提交于 2019-12-25 01:44:50
问题 I have the following aggregator function, which I am not firing in mongo console directly. I am passing this aggregator to an API as a query string. I will encode the following aggregator directly and pass to a get request as a query string. [ { "$limit": 500 }, { "$match": { "timestamp": { "$gte": { "$dte": "2017-07-01" }, "$lt": { "$dte": "2020-12-31" } } } }, { "$group": { "_id": { "$year": "$timestamp" }, "scores": { "$push": "$statement.result.score.scaled" }, "duration_seconds": { "

MongoDB aggregate group on inner child collection and get complete document with count

可紊 提交于 2019-12-24 23:59:54
问题 I have a User collection, which further have a 'UserSubscription' collection, which further have 'Subscription > Publication'. Mongo collection looks like this /* 1 */ { "_id" : 1, "UserSubscriptions" : [ { "_id" : 1, "Subscription" : { "_id" : 1, "Publication" : { "_id" : 1, "Code" : "1MM", }, }, { "_id" : 2, "Subscription" : { "_id" : 2, "Publication" : { "_id" : 2, "Code" : "2MM", }, }, { "_id" : 7, "Subscription" : { "_id" : 7, "Publication" : { "_id" : 1, "Code" : "1MM", }, } ] } /* 2 */

Sum array number values across multiple documents

混江龙づ霸主 提交于 2019-12-24 22:04:03
问题 I have Mongo documents which have array number values in order (it's by day) and I want to sum the same values across multiple documents for each position grouped by field outside of the array. {"_id" : "1", "group" : "A", "value_list" : [1,2,3,4,5,6,7] }, {"_id" : "2", "group" : "B", "value_list" : [10,20,30,40,50,60,70] }, {"_id" : "3", "group" : "A", "value_list" : [1,2,3,4,5,6,7] }, {"_id" : "4", "group" : "B", "value_list" : [10,20,30,40,50,60,70] } So the results I'm after is listed

Mongodb 3.4.7 Aggregation Convert ptms time to seconds

自古美人都是妖i 提交于 2019-12-24 22:00:48
问题 I have the following json: { "statement":{ "result":{ "duration":"PT0.552S", "completion":true, "success":false } }, "statement":{ "result":{ "duration":"PT0.552S", "completion":true, "success":false } }, "statement":{ "result":{ "duration":"PT0.552S", "completion":true, "success":false } } } How can I write a mongo aggregator to getting the statement -> result -> duration time format that now in ptms string to seconds? I need something like: [ 1131, 648, 1678, ..... ..... ] Here each number

MongoDB nested array search using $map

浪子不回头ぞ 提交于 2019-12-24 19:18:43
问题 I have collection which contains nested array. I need to fetch the data based on below condition: empId : 19107 address.country: "AUS" group.primaryGroup.primary:"Y" group.subGroup.primarySubGroup.primary : "Y" Input: { "empId": "19107", "address": [ { "street": "no.12 wilson street", "country":"AUS" }, { "description": "No.32 watson street", "country":"CAN" } ], "mobile": 2387468238, "group": [ { "groupId": 75227, "primaryGroup": [ { "primary": "Y" }, { "primary": "N" } ], "subGroup": [ {

MongoDB How to get value from subdocument when field name is unknown

删除回忆录丶 提交于 2019-12-24 17:11:21
问题 I'm trying to get data from one or more subdocuments but I don't know the name of the field that will hold the subdocument. Here are some examples of what the documents look like. https://github.com/vz-risk/VCDB/blob/master/data/json/0C5DE044-B9B4-408D-9E65-D367EED12AB2.json https://github.com/vz-risk/VCDB/blob/master/data/json/064F5887-C2DA-4139-B3AA-D55906F8C30A.json I would like to get the action varieties for these incidents, so in the case of the first one I would like to get action

GraphQL error returning result of mongoose aggregate command

风流意气都作罢 提交于 2019-12-24 16:10:07
问题 This question is related to the answer posted here I need to return an aggregation result from a GraphQL query. Here is the query code: const companiesWithNoUsers = { type: new GraphQLList(CompanyType), resolve(root, args, context) { return Company.aggregate([ { $lookup: { from: "users", localField: "id", foreignField: "company_id", as: "company_users" } }, { $match: { "company_users:0": { $exists: false } } } ]).exec(); } }; I´m getting the follwing error at GraphQL: Expected value of type \