aggregation-framework

MongoDB get SubDocument

天大地大妈咪最大 提交于 2020-01-09 12:54:27
问题 I would like to retrieve a sub document from a document in MongoDB. I have the following document: { "_id" : "10000", "password" : "password1", "name" : "customer1", "enabled" : true, "channels" : [ { "id" : "10000-1", "name" : "cust1chan1", "enabled" : true }, { "id" : "10000-2", "name" : "cust1chan2", "enabled" : true } ] } The result I would like is: { "id" : "10000-1", "name" : "cust1chan1", "enabled" : true } However, the best I can do so far is using the following query: db.customer

MongoDB get SubDocument

只谈情不闲聊 提交于 2020-01-09 12:54:07
问题 I would like to retrieve a sub document from a document in MongoDB. I have the following document: { "_id" : "10000", "password" : "password1", "name" : "customer1", "enabled" : true, "channels" : [ { "id" : "10000-1", "name" : "cust1chan1", "enabled" : true }, { "id" : "10000-2", "name" : "cust1chan2", "enabled" : true } ] } The result I would like is: { "id" : "10000-1", "name" : "cust1chan1", "enabled" : true } However, the best I can do so far is using the following query: db.customer

MongoDB Document Re-shaping

南笙酒味 提交于 2020-01-09 08:16:39
问题 This question comes out of (as mine usually do) perusing the questions asked on SO and as such, raising another question for myself. So apart from the learning exercise in working towards a solution for a problem, I find that another question pops up, such as this. The original question as yet remains unaccepted by the OP, and indeed has not been clarified as to what "they" wanted to achieve. But I did give my interpretation , in both the simple and long forms of arriving at a solution. The

Using stored JavaScript functions in the Aggregation pipeline, MapReduce or runCommand

爱⌒轻易说出口 提交于 2020-01-09 02:39:04
问题 Is there a way to use a user-defined function saved as db.system.js.save(...) in pipeline or mapreduce? 回答1: Any function you save to system.js is available for usage by "JavaScript" processing statements such as the $where operator and mapReduce and can be referenced by the _id value is was asssigned. db.system.js.save({ "_id": "squareThis", "value": function(a) { return a*a } }) And some data inserted to "sample" collection: { "_id" : ObjectId("55aafd2bacbed38e06f9eccf"), "a" : 1 } { "_id"

Using “aggregate” to combine a list of all subdocuments that match query?

纵饮孤独 提交于 2020-01-07 03:26:26
问题 I'm trying to use a PHP mongo library to "aggregate" on a data structure like this: { "_id": 100, "name": "Joe", "pets":[ { "name": "Kill me", "animal": "Frog" }, { "name": "Petrov", "animal": "Cat" }, { "name": "Joe", "animal": "Frog" } ] }, { "_id": 101, "name": "Jane", "pets":[ { "name": "James", "animal": "Hedgehog" }, { "name": "Franklin", "animal": "Frog" } } For example, if I want to get all subdocuments where the animal is a frog. Note that I do NOT want all matching "super-documents"

How to apply filter for output of aggregation framework of Mongo Db?

浪子不回头ぞ 提交于 2020-01-07 03:11:06
问题 I'm trying to apply condition for the output of the aggregation of Mongo Db but still do not figure out the idea. Here are my sample documents: { 'id':1, 'customerReviews':[5,7,8], 'expertReviews':[9,8,9] }, { 'id':1, 'customerReview':[6,7,7], 'expertReview':[4,8,9] } So if I have a requirement like find all documents which has min(customer_review) > 5 -> only the second document is correct. Here is my initial point which get the min(customerReview) of documents: db.getCollection('subscriber'

How to write redact aggregation in java?

淺唱寂寞╮ 提交于 2020-01-06 21:13:23
问题 I'm trying to convert MongoDb query using aggregation framework using Java driver. I have been helped to create query here How to apply filter for output of aggregation framework of Mongo Db?. Here is the sample aggregate query: db.movies.aggregate( [{ $redact: { $cond: { if: {$gt: [{ $avg: "$customerReviews"}, 7 ] }, then: "$$KEEP", else: "$$PRUNE" } } }, {$skip:1}, {$limit:2} ] ); I started with: BasicDBObject avgQuery = new BasicDBObject("$avg", "$customerReviews"); But cannot figure out

Mongodb aggregation - first create item list and get intersect of items

大城市里の小女人 提交于 2020-01-06 19:27:11
问题 I have the Documents as follows, { "_id" : ObjectId("5539d45ee3cd0e48e99c3fa6"), "userId" : 1, "movieId" : 6, "rating" : 2.0000000000000000, "timestamp" : 9.80731e+008 } then I need to get the common(intersect) items for given two users (like userId:1 and userId:2) for example, { "_id" : ObjectId("5539d45ee3cd0e48e99c1fa7"), "userId" : 1, "movieId" : 22, "rating" : 3.0000000000000000, "timestamp" : 9.80731e+008 }, { "_id" : ObjectId("5539d45ee3cd0e48e99c1fa8"), "userId" : 1, "movieId" : 32,

Aggregation using MongoDB java driver with multiple $project

喜夏-厌秋 提交于 2020-01-06 05:25:37
问题 I am trying to convert Mongo Aggregation query into Java and I am kind of stuck on the second $project statement db.ValidationResults.aggregate([ { $match: { "_id.settlementInstanceId" : "715e66c7-92ff-4619-9324-2c708489fe27", "Versions.scope" : "INPUT_QUALITY" } }, { $project: { Versions: 1, MaxVersion: { $max: "$Versions.version" } } }, { $project: { Versions: { $filter: { input: "$Versions", as: "x", cond: { $eq: [ "$$x.version", "$MaxVersion" ] } } } } }, { $unwind: "$Versions" } ]) Any

MongoDB very slow when using $group function

这一生的挚爱 提交于 2020-01-06 04:36:42
问题 MongoDB seems very slow when adding a $group pipeline to simply count the number of records for each occurrence. Below is an example of running it without $group , using $lookup perform a join. This runs in less than 1 second. Note: I'm doing the lookup so I can eventually count the attendance code type for each student as well as the total db.attendances.aggregate( [ { $lookup: { from: 'attendance_codes', localField: 'attendance_code', foreignField: '_id', as: 'attendance_code' } }, {