aggregation-framework

How to group query with multiple $cond?

天涯浪子 提交于 2019-12-20 09:33:47
问题 I want to query like below, but this contains only one $cond . How to query with two $cond ? collection.aggregate( { $match : { '_id' : {$in:ids} } }, { $group: { _id: '$someField', ... count: {$sum: { $cond: [ { $eq: [ "$otherField", false] } , 1, 0 ] }} } }, function(err, result){ ... } ); 回答1: You want to use a compound expression inside {$cond:[]} - something like: collection.aggregate( { $match : { '_id' : {$in:ids} } }, { $group: { _id: '$someField', ... count: {$sum: { $cond: [ {$and :

MongoDB query with conditional group by statement

故事扮演 提交于 2019-12-20 07:56:28
问题 I need to export customer records from database of mongoDB. Exported customer records should not have duplicated values. "firstName+lastName+code" is the key to DE-duped the record and If there are two records present in database with same key then I need to give preference to source field with value other than email. customer ( id,firstName,lastName,code,source ) collection is this. If there are record 3 records with same unique key and 3 different sources then i need to choose only one

How to sort with the sum of 2 fields in MongoDB

十年热恋 提交于 2019-12-20 07:38:39
问题 I have this document: { "_id": "59b804e1ee8a4071a5ea3fcc", "description" : "description", "imagepath" : "https://example.com", "type" : "label", "downvotes" : -25, "upvotes" : 15, "language" : "en", "approoved" : true }, { "_id": "59b804e1ee8a4071a5ea4dfd", "description" : "description", "imagepath" : "https://example.com", "type" : "label", "downvotes" : 0, "upvotes" : 30, "language" : "en", "approoved" : true } How to sort these 2 objects by the sum of the upvotes & downvotes values?

How to use aggregation function mongo db-query

做~自己de王妃 提交于 2019-12-20 07:29:08
问题 I am new in MongoDB and I would like to use the aggregation function where I want to check type == topic and get the following output Expected output [ { conceptName : 59d98cfd1c5edc24e4024d00 totalCount : 2 }, { conceptName : 59d98cfd1c5edc24e4024d03 totalCount : 1 } ] Sample input db.GroupContents { "_id" : "5a0948bb1c5edc7a5000521a", "type" : "topic", "groupID" : "5a0948bb1c5edc7a5000521a", "pedagogyID" : "59d98cfa1c5edc24e40249a3", } Sample input db.PedagogyNodes { "_id" :

Count Elements SubDocument that match a given criterion

吃可爱长大的小学妹 提交于 2019-12-20 07:12:43
问题 I have the following document structure in mongodb { "_id" : "123", "first_name" : "Lorem", "last_name" : "Ipsum", "conversations" : { "personal" : [ { "last_message" : "Hello bar", "last_read" : 1474456404 }, { "last_message" : "Hello foo", "last_read" : 1474456404 }, ... ], "group" : [ { "last_message" : "Hello Everyone", "last_read" : null } ... ] } } I want to count the number of conversations from the sub arrays, personal and group where the last_read is null, for a given user. Please

MongoDB querying from Aggregation to Group

拜拜、爱过 提交于 2019-12-20 06:44:45
问题 I have this following query in MongoDB: var subquery = {"shipdate" : { "$gte" : 19960101, "$lt" : 19960401 } }; var eachsupp = db.lineitems.aggregate([ { $match : subquery }, { $project : { "_id" : 0, "revenue" : {$multiply : ["$extendedprice", {$subtract : [1, "$discount"] }] }, "supplier_no" : "$partsupp.supplier.suppkey", "name" : "$partsupp.supplier.name", "address" : "$partsupp.supplier.address", "phone" : "$partsupp.supplier.phone" }}, { $group : { _id : "$supplier_no", total_revenue :

Subtract $sum from Sub-document

别说谁变了你拦得住时间么 提交于 2019-12-20 06:27:26
问题 Given the following record in my MongoDB table: { "_id" : ObjectId("5a00c1c71680084c55811ae2"), "name" : "test", "tenantId" : "paul", "price" : 300, "deposits" : [ { "amount" : 100, "date" : ISODate("2017-11-07T14:08:19.324Z"), "_id" : ObjectId("5a01be55424b0f8922a5b472") }, { "amount" : 50, "date" : ISODate("2017-11-87T14:08:19.324Z"), "_id" : ObjectId("5a01be55424b0f8922a5b473") } ], "attention" : "", "due" : ISODate("2017-10-26T22:00:00.000Z") } I would like to filter all the records with

How to search sub arrays in MongoDB

♀尐吖头ヾ 提交于 2019-12-20 05:41:23
问题 I have this MongoDB collection: { "_id" : ObjectId("123"), "from_name" : "name", "from_email" : "email@mxxxx.com", "to" : [ { "name" : "domains", "email" : "domains@xxx.com" } ], "cc" : [ ], "subject" : "mysubject" } My goal is to search in this collection by the "to" with some email. 回答1: If you only want one field then MongoDB has "dot notation" for accessing nested elements: db.collection.find({ "to.email": "domains@example.com" }) And this will return documents that match: For more that

Find Count of all Overlapping Intervals

坚强是说给别人听的谎言 提交于 2019-12-20 05:39:09
问题 I have startTime and endTime for all records like this: { startTime : 21345678 endTime : 31345678 } I am trying to find number of all the conflicts. For example if there are two records and they overlap the number of conflict is 1. If there are three records and two of them overlap the conflict is 1. If there are three records and all three overlap the conflicts is 3 i.e [(X1, X2), (X1, X3), (X2, X3)] As an algorithm I am thinking of sorting the data by start time and for each sorted record

Remove Duplicates on mongodb

流过昼夜 提交于 2019-12-20 05:29:11
问题 I would like to remove duplicates on robomongo, my version 3.0.12 so I cant use DropDups, { "_id" : ObjectId("id"), "Name" : "No One", "SituationDate" : "18-03-2017", "Situation" : "ACTIVE", "Region" : "13 REGION", "RegisterNumber" : "7649", "Version" : "20170517" } The RegisterNumber should be unique so I would like to remove as duplicates by the RegisterNumber. EDIT :I just discovered that people from different regions can have the same registerNumber... How can I remove only those who have