aggregation-framework

Reshape all the documents in the collection

扶醉桌前 提交于 2019-12-30 10:39:37
问题 I have the following structure in my documents: { "_id" : 1, "item" : { "name" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-03-01T08:00:00Z") } } And I want to transform each document on this: { "_id" : 1, "name" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-03-01T08:00:00Z") } In other words remove the embedded document but not the details! Thanks! 回答1: You can use the aggregation especially the $projectoperator for that. The $out operator let you write the

mongo $slice query reverse index out of range

余生长醉 提交于 2019-12-30 10:08:14
问题 The following query in mongo, behaves strange : db.items.findOne({},{ "List": { "$slice": [ skip, 3 ] }}) First: Instead of returning one object with ["_id","List"] keys only, it returns a full object. Second: if skip is negative and |skip| is higher than list.length then it returns the first three elements as though skip==0 I would expect for: { "_id" : ObjectId("542babf265f5de9a0d5c2928"), "List" : [ 1, 2, 3, 4, 5 ] "other" : "not_important" } query: db.items.findOne({},{ "List": { "$slice"

mongo $slice query reverse index out of range

一个人想着一个人 提交于 2019-12-30 10:08:05
问题 The following query in mongo, behaves strange : db.items.findOne({},{ "List": { "$slice": [ skip, 3 ] }}) First: Instead of returning one object with ["_id","List"] keys only, it returns a full object. Second: if skip is negative and |skip| is higher than list.length then it returns the first three elements as though skip==0 I would expect for: { "_id" : ObjectId("542babf265f5de9a0d5c2928"), "List" : [ 1, 2, 3, 4, 5 ] "other" : "not_important" } query: db.items.findOne({},{ "List": { "$slice"

MongoDB sum arrays from multiple documents on a per-element basis

妖精的绣舞 提交于 2019-12-30 10:06:12
问题 I have the following document structure (simplified for this example) { _id : ObjectId("sdfsdf"), result : [1, 3, 5, 7, 9] }, { _id : ObjectId("asdref"), result : [2, 4, 6, 8, 10] } I want to get the sum of those result arrays, but not a total sum, instead a new array corresponding to the sum of the original arrays on an element basis, i.e. result : [3, 7, 11, 15, 19] I have searched through the myriad questions here and a few come close (e.g. this one, this one, and this one), but I can't

Aggregate Count Array Members Matching Condition

淺唱寂寞╮ 提交于 2019-12-30 09:43:07
问题 I'm having some trouble, as stated in title, to count elements in an Array using MongoDB. I have a DB with only one document, made as follow: {_id: ObjectId("abcdefghilmnopq"), "Array": [ {field1: "val1", field2: "val2", field3: "val3", ... }, {field1: "Value1", field2: "Value2", field3: "Value3", ... }, ... ] } I wanna count the number of elements of the array which have a certain condition (e.g. field1: "a" , and count all elements which have field1 = a ). I'm trying with this code: db

Spring - mongodb - aggregation - The 'cursor' option is required

怎甘沉沦 提交于 2019-12-30 09:38:28
问题 Executing the following aggregation pipeline: public void getMostLikedItems () { UnwindOperation unwind = Aggregation.unwind("favoriteItems"); GroupOperation group = Aggregation.group("favoriteItems").count().as("likes"); SortOperation sort = Aggregation.sort(Sort.Direction.DESC, "likes"); Aggregation aggregation = newAggregation(unwind, group, sort); DBObject result = mongoTemplate.aggregate(aggregation, "users", LikedItem.class).getRawResults(); } throws the following exception: com.mongodb

MongoDb aggregation $match error : “Arguments must be aggregate pipeline operators”

北城余情 提交于 2019-12-30 06:03:32
问题 I can get all stats of the site with aggregation but I want to it for a certain user, like $where . All stats: games.aggregate([{ $group: { _id: '$id', game_total: { $sum: '$game_amount'}, game_total_profit: { $sum: '$game_profit'}} }]).exec(function ( e, d ) { console.log( d ) }) When I try to use $match operator, I'm getting error : games.aggregate([{ $match: { '$game_user_id' : '12345789' }, $group: { _id: '$id', game_total: { $sum: '$game_amount'}, game_total_profit: { $sum: '$game_profit

Mongodb distinct on a array field with regex query?

纵然是瞬间 提交于 2019-12-30 05:03:25
问题 Basically i'm trying to implement tags functionality on a model. > db.event.distinct("tags") [ "bar", "foo", "foobar" ] Doing a simple distinct query retrieves me all distinct tags. However how would i go about getting all distinct tags that match a certain query? Say for example i wanted to get all tags matching foo and then expecting to get ["foo","foobar"] as a result? The following queries is my failed attempts of achieving this: > db.event.distinct("tags",/foo/) [ "bar", "foo", "foobar"

How to sort sub-documents in the array field?

随声附和 提交于 2019-12-29 09:14:25
问题 I'm using the MongoDB shell to fetch some results, ordered. Here's a sampler, { "_id" : "32022", "topics" : [ { "weight" : 281.58551703724993, "words" : "some words" }, { "weight" : 286.6695125796183, "words" : "some more words" }, { "weight" : 289.8354232846977, "words" : "wowz even more wordz" }, { "weight" : 305.70093587160807, "words" : "WORDZ" }] } what I want to get is, same structure, but ordered by "topics" : [] { "_id" : "32022", "topics" : [ { "weight" : 305.70093587160807, "words"

Absolute value with MongoDB aggregation framework

回眸只為那壹抹淺笑 提交于 2019-12-29 08:33:09
问题 I'm using the MongoDB aggregation framework and need to take the absolute value of an amount field, which I use in both the project portion and the group portion, ex: '$project' => { 'amount' => 1, '_id' => 0 } .... '$group' => { 'total' => {'$sum' => '$amount'} } How do you take the absolute value of the 'amount' field in this case? I wasn't able to find it in the docs (maybe it's not available?) 回答1: It's not directly available, but you can do it using a $cond operator and a $subtract