aggregation-framework

Using maxTimeMS parameter with aggregation queries on Mongo 2.6 and Pymongo 2.7.1

一曲冷凌霜 提交于 2019-12-20 04:53:38
问题 I'm unable to use maxTimeMS parameter with Mongo 2.6 and Pymongo 2.7.1 As per the documentation on this page Official Mongodb Aggregation Page the aggregation method should return a Cursor object. However, when I run the query locally on a mongod instance (2.6+) with pymongo 2.7.1, I get a dict object! In [14]: obj = coll.aggregate({'$group': {'_id': '$l', 'n': {'$sum': 1}}}) In [15]: type(obj) Out[15]: dict Can anyone help me understand what is happening here? 回答1: Yes, you can use maxTimeMS

Using maxTimeMS parameter with aggregation queries on Mongo 2.6 and Pymongo 2.7.1

半城伤御伤魂 提交于 2019-12-20 04:53:04
问题 I'm unable to use maxTimeMS parameter with Mongo 2.6 and Pymongo 2.7.1 As per the documentation on this page Official Mongodb Aggregation Page the aggregation method should return a Cursor object. However, when I run the query locally on a mongod instance (2.6+) with pymongo 2.7.1, I get a dict object! In [14]: obj = coll.aggregate({'$group': {'_id': '$l', 'n': {'$sum': 1}}}) In [15]: type(obj) Out[15]: dict Can anyone help me understand what is happening here? 回答1: Yes, you can use maxTimeMS

Aggregate documents where objects in array matches multiple conditions

﹥>﹥吖頭↗ 提交于 2019-12-20 04:19:25
问题 I have a collection with documents similar to such: { "_id": ObjectId("xxxxx"), "item": [ { "property": ["attr1", "+1"] }, { "property": ["attr2", "-1"] } ] } { "_id": ObjectId("xxxxy"), "item": [ { "property": ["attr1", "-1"] }, { "property": ["attr2", "0"] } ] } { "_id": ObjectId("xxxxz"), "item": [ { "property": ["attr1", "0"] }, { "property": ["attr2", "+1"] } ] } Preferably using an aggregation pipeline, is there any way to match the document if and only if any one of the properties

Projection option to return length/size of field

我的梦境 提交于 2019-12-20 04:16:31
问题 I'm simply trying to write a mongo query like in SQL: SELECT LENGTH(binaryBody) AS bodyLength FROM documents; To do so I thought I have to use the projection of the find method. db.documents.find( {<query>}, { bodyLength: {$size: 'body'}} ); But how to do that? Error: error: { "waitedMS" : NumberLong(0), "ok" : 0, "errmsg" : "Unsupported projection option: bodyLength: { $size: \"body\" }", "code" : 2 } 回答1: .find() does not "alter" documents returned in any way. You can only "include" or

How to return a nested document in an array

此生再无相见时 提交于 2019-12-20 04:14:01
问题 I have documents with a schema such as follows: { "user_id": 123, "services":[ {"name": "test", "data": ... }, {"name": "test1", "data": ... }, {"name": "test2", "data": ... } ] } I'm trying to get a service by name for a specific user_id returned as follows: {"name": "test2", "data": ... } I'm having difficulty wrapping my head around how to do this and seems an aggregation shouldn't be needed for something as simple as this but maybe I'm wrong. I'm sure a projection would work in a find_one

$expr arrayElementAt not working in aggregation for embedded document

守給你的承諾、 提交于 2019-12-20 03:56:09
问题 I am doing mongo db aggregation like $cursor = $this->collection->aggregate( array( array( '$project' => array( 'FullName' => array('$concat' => array('$first_name', ' ', '$middle_name', ' ', '$last_name')), 'FirstMiddle' => array('$concat' => array('$first_name', ' ', '$middle_name')), 'FirstLast' => array('$concat' => array('$first_name', ' ', '$last_name')), 'FirstName' => array('$concat' => array('$first_name')), 'MiddleName' => array('$concat' => array('$middle_name')), 'LastName' =>

Spring Data-MongoDb Options for aggregation method

北慕城南 提交于 2019-12-20 03:29:44
问题 How can i set " allowDiskUse " option in aggregation method in spring data-mongodb framework ? 回答1: The core aggregation abstraction in Spring Data MongoDB is - as the name suggests - Aggregation . It exposes a fluent API to build up a pipeline using aggregation operations. As of version 1.6.0.M1 the Aggregation class has a ….withOptions(…) method to be used like this: Aggregation aggregation = newAggregation(…) // build up pipeline in here .withOptions(newAggregationOptions().allowDiskUse

Analog for group concat in sql

这一生的挚爱 提交于 2019-12-20 03:28:06
问题 In an aggregation process I've got this data: { "_id" : "billing/DefaultController/actionIndex", "min_time" : 0.033, "max_time" : 5.25, "exec_time" : 555.490999999997, "qt" : 9059, "count" : 2, "date" : [ ISODate("2014-02-10T00:00:00.000Z"), ISODate("2014-02-11T00:00:00.000Z") ] }, How to change my query: db.page_speed_reduced.aggregate([ {$group: { _id: "$value.route", min_time: {$min: "$value.min_time"}, max_time: {$max: "$value.max_time"}, exec_time: {$sum: "$value.exec_time"}, qt: {$sum:

Find sum of fields inside array in MongoDB

梦想的初衷 提交于 2019-12-20 03:18:01
问题 I have a data as follows: > db.PQRCorp.find().pretty() { "_id" : 0, "name" : "Ancy", "results" : [ { "evaluation" : "term1", "score" : 1.463179736705023 }, { "evaluation" : "term2", "score" : 11.78273309957772 }, { "evaluation" : "term3", "score" : 6.676176060654615 } ] } { "_id" : 1, "name" : "Mark", "results" : [ { "evaluation" : "term1", "score" : 5.89772766299929 }, { "evaluation" : "term2", "score" : 12.7726680028769 }, { "evaluation" : "term3", "score" : 2.78092882672992 } ] } { "_id" :

Mongo groupby month using UNIX millisecond time

房东的猫 提交于 2019-12-20 03:16:08
问题 I have a Mongo collection that looks as follows: [{ id: 1, timestamp: 1534488870841, type: 'deposit' }, { id: 1, timestamp: 1534488915119, type: 'deposit' }] How can I make a query to list all deposit transactions, grouped by the month. The month must be calculated using the timestamp attribute (UNIX millisecond). I can get the deposit's as follows but I am unsure on how to group: db.getCollection('transactions').find( {"type":"deposit"}); Edit : Mongo version 3.4 回答1: You can try below