aggregation-framework

Compare two array fields in same document

强颜欢笑 提交于 2020-02-23 07:36:09
问题 I have two different Arrays with Data and I have to merge two arrays same object in one object how can I merge with help of MongoDB Aggregation. here are my two Arrays {"ids" : [ { "_id" : ObjectId("5ba8d8dfaa988532967029af"), "level" : 2, "completed" : 5, "asset" : ObjectId("5ba8caa1aa98853296702989") }, { "_id" : ObjectId("5ba8d8dfaa988532967029b0"), "level" : 2, "completed" : 3, "asset" : ObjectId("5ba8caf6aa9885329670298a") }, { "_id" : ObjectId("5ba8d8dfaa988532967029b1"), "level" : 2,

Compare two array fields in same document

时间秒杀一切 提交于 2020-02-23 07:34:21
问题 I have two different Arrays with Data and I have to merge two arrays same object in one object how can I merge with help of MongoDB Aggregation. here are my two Arrays {"ids" : [ { "_id" : ObjectId("5ba8d8dfaa988532967029af"), "level" : 2, "completed" : 5, "asset" : ObjectId("5ba8caa1aa98853296702989") }, { "_id" : ObjectId("5ba8d8dfaa988532967029b0"), "level" : 2, "completed" : 3, "asset" : ObjectId("5ba8caf6aa9885329670298a") }, { "_id" : ObjectId("5ba8d8dfaa988532967029b1"), "level" : 2,

How to get the total page number for pagination

我们两清 提交于 2020-02-06 08:01:15
问题 I try to make my pageable data for weeks everything works but I would like to know the total number of pagination pages here are the data of my mongonDB. { "content": [ { "mame": "iPhone X", "eategory": "High-Tech", "productId": "aDPXF7Xq", "details": { "vating": "00", "stocks": "20", "price": "800000", "tags": [ { "tagsl": "Apple", "tags2": "aull", "tags3": null } ], "brand": "Apple", "description": "Iphone xX", "picture": [ { "picturel": "photol", "picture2": null, "picture3": null,

MongoDB seems to choose the wrong index when doing aggregate

被刻印的时光 ゝ 提交于 2020-02-05 13:50:31
问题 A testing mongodb(version 3.0.1) running on Amazon EC2(3.14.33-26.47.amzn1.x86_64, t2.medium: 2 vcpus, 4G mem). And a collection "access_log"(about 40,000,000 records, 1,000,000 each day), and some indexes on it: ... db.access_log.ensureIndex({ visit_dt: 1, 'username': 1 }) db.access_log.ensureIndex({ visit_dt: 1, 'file': 1 }) ... When doing following "aggregate", it's extremely slow(takes several hours): db.access_log.aggregate([ { "$match": { "visit_dt": { "$gte": ISODate('2015-03-09'), "

MongoDB seems to choose the wrong index when doing aggregate

五迷三道 提交于 2020-02-05 13:48:32
问题 A testing mongodb(version 3.0.1) running on Amazon EC2(3.14.33-26.47.amzn1.x86_64, t2.medium: 2 vcpus, 4G mem). And a collection "access_log"(about 40,000,000 records, 1,000,000 each day), and some indexes on it: ... db.access_log.ensureIndex({ visit_dt: 1, 'username': 1 }) db.access_log.ensureIndex({ visit_dt: 1, 'file': 1 }) ... When doing following "aggregate", it's extremely slow(takes several hours): db.access_log.aggregate([ { "$match": { "visit_dt": { "$gte": ISODate('2015-03-09'), "

Apply uppercase in all the fields of all documents (MongoDB - aggregate)

假装没事ソ 提交于 2020-02-05 04:25:07
问题 Suppose that I have documents like these: [ { "one": "aaa", "two": "bbb", "three": "aba" }, { "one": "dd", "two": "cc", } ] Is there a way (with aggregate) to apply a function to every field? Like this? db.collection.aggregate([ { '$project': { 'SOME FUNCTION TO UPPERCASE ALL THE FIELDS IN A ONCE' } } ]) Expected result : [ { "one": "AAA", "two": "BBB", "three": "ABA" }, { "one": "DD", "two": "CC", } ] 回答1: Please try this : db.collection.aggregate([{ /** Adding a new field data with required

Mongodb Aggregation by 5 minutes

☆樱花仙子☆ 提交于 2020-02-04 05:37:47
问题 I have IoT sensors which keep sending events every second, and storing the events in MongoDB below format: { "_id" : "stp_23", "HP" : [ 1261.0, 1357.0, 1337.0, 1250.0, 1269.0, 1134.0, 1219.0, 1269.0, 1166.0, 1361.0, 1246.0, ... ] "TS" : [ "2019-12-20T00:00:04.6440124Z", "2019-12-20T00:00:14.6440124Z", "2019-12-20T00:00:24.6440124Z", "2019-12-20T00:00:34.6450042Z", "2019-12-20T00:00:44.6450042Z", "2019-12-20T00:00:54.6450042Z", "2019-12-20T00:01:55.6460113Z", "2019-12-20T00:02:05.6460113Z",

MongoDB Calculate Values from Two Arrays, Sort and Limit

一笑奈何 提交于 2020-02-03 12:14:13
问题 I have a MongoDB database storing float arrays. Assume a collection of documents in the following format: { "id" : 0, "vals" : [ 0.8, 0.2, 0.5 ] } Having a query array, e.g., with values [ 0.1, 0.3, 0.4 ] , I would like to compute for all elements in the collection a distance (e.g., sum of differences; for the given document and query it would be computed by abs(0.8 - 0.1) + abs(0.2 - 0.3) + abs(0.5 - 0.4) = 0.9 ). I tried to use the aggregation function of MongoDB to achieve this, but I can

Mongoose: how to use index in aggregate?

我们两清 提交于 2020-02-02 16:14:45
问题 How can I use indexes in aggregate? I saw the document https://docs.mongodb.com/manual/core/aggregation-pipeline/#pipeline-operators-and-indexes The $match and $sort pipeline operators can take advantage of an index when they occur at the beginning of the pipeline. Is there any way of using index not the beginning situation? like $sort , $match or $group Please help me 回答1: An index works by keeping a record of certain pieces of data that point to a given record in your collection. Think of

Mongoose: how to use index in aggregate?

岁酱吖の 提交于 2020-02-02 16:14:32
问题 How can I use indexes in aggregate? I saw the document https://docs.mongodb.com/manual/core/aggregation-pipeline/#pipeline-operators-and-indexes The $match and $sort pipeline operators can take advantage of an index when they occur at the beginning of the pipeline. Is there any way of using index not the beginning situation? like $sort , $match or $group Please help me 回答1: An index works by keeping a record of certain pieces of data that point to a given record in your collection. Think of