aggregation-framework

How to merge multiple fields in a collection?

廉价感情. 提交于 2020-01-06 03:30:08
问题 Example entry: { "_id" : "00-01@mail.ru", " pass" : 123654, "field2" : 235689, "field3" : "cccp123654", "field4" : "lhfrjy" } Desired result: { "_id" : "00-01@mail.ru", " pass" : 123654, 235689, "cccp123654", "lhfrjy" } I want to have two final fields (_id and pass). I have attempted the following: db.emails.aggregate([ { "$project": { "pass": { "$setUnion": [ "$field2", "$field3" ] } }} ]) However, this results in the following error: 2018-01-22T03:01:26.074+0000 E QUERY [thread1] Error:

How to parse extended JSON Date in aggregation pipeline using ParseExtJSONArray() in mongo-go-driver

两盒软妹~` 提交于 2020-01-06 02:46:14
问题 I've got a collection with a Date field: { "_id" : ObjectId("5b92b359ddceef5b24502834"), "dateTimeGMT" : ISODate("2018-08-22T09:29:25.000Z"), yada, yada, yada } I'm trying to find by date in a $match aggregation stage with the ParseExtJSONArray function of mongo-go-driver. (I am aware of how to do this with *bson.Array directly. I'm asking so I know the right way to do it with ParserExtJSONArray or if I've run up against a limitation.) I've simplified to this example and confirmed it is

Using Mongo aggregation to calculate sum of values

霸气de小男生 提交于 2020-01-06 02:24:09
问题 I have invoices, where each invoice contains a list of items. Each item has (among other things) the following fields: name qty total And each invoice has (among other things) the fields: _id created items The invoices live in a dedicated Mongo collection, called invoices . I would like to get all the invoices containing the specified item, where for each invoice the following information needs to be returned: _id created qty (of the given item) total (of the given item) Let us call the tuple

How to join two collection in mongo without lookup

最后都变了- 提交于 2020-01-05 12:58:34
问题 I have two collection, there name are post and comment . The model structure is in the following. I want to use aggregation query post and sort by comments like length sum, currently I can query a post comments like length sum in the following query statement. My question is how can I query post and join comment collection in Mongo version 2.6 . I know after Mongo 3.2 have a lookup function. I want to query post collection and sort by foreign comments likes length. Is it have a best way to do

Behaviour of the project stage operator in projecting Arrays

馋奶兔 提交于 2020-01-05 09:42:13
问题 My question is closely related to this, but not similar. I have a sample document in my collection: db.t.insert({"a":1,"b":2}); My intent is to project a field named combined of type array with the values of both a and b together. ([1,2]) . I simply try to aggregate with a $project stage: db.t.aggregate([ {$project:{"combined":[]}} ]) MongoDB throws an error: disallowed field type Array in object expression . Which means a field cannot be projected as a array. But when i use a $cond operator

Behaviour of the project stage operator in projecting Arrays

北城以北 提交于 2020-01-05 09:41:51
问题 My question is closely related to this, but not similar. I have a sample document in my collection: db.t.insert({"a":1,"b":2}); My intent is to project a field named combined of type array with the values of both a and b together. ([1,2]) . I simply try to aggregate with a $project stage: db.t.aggregate([ {$project:{"combined":[]}} ]) MongoDB throws an error: disallowed field type Array in object expression . Which means a field cannot be projected as a array. But when i use a $cond operator

Temporary Collection in MongoDB

我怕爱的太早我们不能终老 提交于 2020-01-05 09:07:40
问题 I can't understand this paragraph from mongodb MapReduce documentation (http://docs.mongodb.org/manual/applications/map-reduce/) - what Temporary Collection (optimisation?) is good for (business case, benefits etc)? Temporary Collection The map-reduce operation uses a temporary collection during processing. At completion, the map-reduce operation renames the temporary collection. As a result, you can perform a map-reduce operation periodically with the same target collection name without

Sum of subdocuments in Mongoose

北战南征 提交于 2020-01-05 07:19:09
问题 Currently I have this schema. var cartSchema = new Schema({ userPurchased: {type: Schema.Types.ObjectId, ref: 'users'}, products: [ { product: {type: Schema.Types.ObjectId, ref: 'products'}, size: {type: String, required: true}, quantity: {type: Number, required: true}, subTotal: {type: Number, required: true} } ], totalPrice: {type: Number, default: 0, required: true} }); Example of db record { "_id": { "$oid": "586f4be94d3e481378469a08" }, "userPurchased": { "$oid":

Mongo / Mongoose - Aggregating by Date

假装没事ソ 提交于 2020-01-05 05:52:44
问题 I have a mongo/mongoose schema which when queried retruns documents such as { "_id" : ObjectId("5907a5850b459d4fdcdf49ac"), "amount" : -33.3, "name" : "RINGGO", "method" : "VIS", "date" : ISODate("2017-04-26T23:00:00Z"), "importDate" : ISODate("2017-05-01T21:15:49.581Z"), "category" : "Not Set", "__v" : 0 } { "_id" : ObjectId("5907a5850b459d4fdcdf49ba"), "amount" : -61.3, "name" : "Amazon", "method" : "VIS", "date" : ISODate("2017-03-23T00:00:00Z"), "importDate" : ISODate("2017-05-01T21:15:49

Group by time interval between window of time

只愿长相守 提交于 2020-01-05 05:00:42
问题 I have the following document structure: { "_id":"5c59c35d8610f702d00e6f70", "ipAddress":"50.116.14.48", "startTime":"2018-02-06T12:01:59.000Z", "endTime":"2018-02-06T12:31:00.000Z", } I would like to be able to group occurrences within a 15 min window of time. For example; Given the structure of the document above I would assume this document would count as an occurrence for both 0-15mins, 15-30mins, and 30-45mins. The result would look something like this: [ { "occurrences":1, "startWindow"