aggregation-framework

Query the count of sub elements with condition in Mongo DB

会有一股神秘感。 提交于 2020-01-24 19:51:05
问题 I am new to Mongo DB and would appreciate some help with the following query task. I have a collection of documents that looks like below: { "field_1" : { "subfield_1" : { "subsubfield_1" : "true", "subsubfield_2" : "false", "subsubfield_3" : "true" }, "subfield_2" : "sf2" }, "field_2" : { "subfield_1" : { "subsubfield_1" : "true", "subsubfield_2" : "false" }, "subfield_2" : "sf2" }, "field_3" : { "subfield_1" : { "subsubfield_1" : "true", "subsubfield_2" : "false", "subsubfield_3" : "false"

Mongodb, $sum with condition

独自空忆成欢 提交于 2020-01-24 19:03:37
问题 Documents: [ { name: 'abc' length: 25, area: 10 }, { name: 'abc', length: 5 } ] Output after aggregate query: [ { count: 2, summarizedLength: 30, summarizedArea: null, _id: { name: 'abc' } } ] The length and area should summarized. But only if all documents have the area or length property. So if any length property is missing by the grouped properties, the summarizedLength value should be null/undefined/not exisitng , and same with area . I tried this: let query = mongoose.model(

MongoDB aggregate count of items in two arrays across different documents?

匆匆过客 提交于 2020-01-24 17:33:07
问题 Here is my MongoDB collection schema: company: String model: String cons: [String] // array of tags that were marked as "cons" pros: [String] // array of tags that were marked as "pros" I need to aggregate it so I get the following output: [{ "_id": { "company": "Lenovo", "model": "T400" }, "tags": { tag: "SomeTag" pros: 124 // number of times, "SomeTag" tag was found in "pros" array in `Lenovo T400` cons: 345 // number of times, "SomeTag" tag was found in "cons" array in `Lenovo T400` } }...

MongoDB: Find objects with field names starting with

时间秒杀一切 提交于 2020-01-24 11:11:11
问题 Query for a MongoDB: From a given collection (see example below) I need only objects listed that comprise fields, where the field name starts with "need_". Example of a collection with three objects /* 1 */ { "_id" : 1, "need_some" : "A", "need_more" : 1, "website_id" : "123456789" } /* 2 */ { "_id" : 2, "need_more" : 2, "website_id" : "123456789" } /* 3 */ { "_id" : 3, "website_id" : "123456789" } Desired Output: /* 1 */ { "_id" : 1, "need_some" : "A", "need_more" : 1, "website_id" :

MongoDB get individual count from sudocuments with comparison operations

冷暖自知 提交于 2020-01-23 17:27:06
问题 I am using mongoDB I use query db.getCollection('managements').find({'managers.role':"HR"},{'managers.$':1}) I get result as below /* 1 */ { "_id" : ObjectId("1"), "managers" : [ { "id" : "100", "role" : "HR" }, { "id" : "101", "role" : "ADMIN" }, { "id" : "102", "role" : "ADMIN" } ] } /* 2 */ { "_id" : ObjectId("2"), "managers" : [ { "id" : "104", "role" : "ADMIN" }, { "id" : "105", "role" : "HR" } , { "id" : "106", "role" : "HR" } , { "id" : "107", "role" : "HR" } ] } Now I want to get

MongoDB: can't convert from BSON type EOO to Date

一笑奈何 提交于 2020-01-23 05:22:32
问题 I' trying to use aggregation framework (with ruby) and project the date like this: db['requests'].aggregate([ {"$project" => { _id: 0, method: '$method', user: '$user', year: {'$year' => '$timestamp'} }}]) the document is like this one: { _id: ObjectId("5177d7d7df26358289da7dfd"), timestamp: ISODate("2013-04-12T03:58:05+00:00"), method: "POST", status: "200", inputsize: "874", outputsize: "4981", user: "131" } but i get the following error: Mongo::OperationFailure: Database command 'aggregate

How to use $hint in MongoDB aggregation query?

余生颓废 提交于 2020-01-23 05:13:18
问题 I am using mongo v3.0.1 on a ubuntu machine. And I have a collection of 300million rows. I have created two indexes based on my query preference. When I am trying to run aggregation with explain, It is taking the inefficient index, and that is why it's taking 20-25 secs more time. Is there any way to put $hint , so that my aggregation query use the appropriate index. $match is in my first pipeline stage. I have two indexes: "Host_-1_SiteType_-1" "VisitTime_-1_AccountId_-1_Host_-1_SiteType_-1

MongoDB Aggregate & Grouping Issue in MeteorJS

泪湿孤枕 提交于 2020-01-21 19:21:31
问题 Using MongoDB in Meteor JS, how do you use Meteor Aggregate properly? The intended result is to return grouped users by their userId and sum up a boolean field called "progressState" (true/false). For example the document can have: user 001 - true user 001 - false user 001 - true user 003 - false user 005 - true but the intended result would be: user 001: 2 true user 003: 0 true user 005: 1 true etc.. My attempt gives the following error: "exception: FieldPath field names may not start with '

MongoDB : How to multiply a field that appears only in $project?

≡放荡痞女 提交于 2020-01-21 19:13:10
问题 I'm joining ($lookup) two collection the following way : const LEAD_PRICE = ....; // doesn't matter Client.aggregate( [ { $lookup: { from: "clientboughtleads", localField: "_id", foreignField: "Client", as: "ClientLeads" } }, { $project: { ClientId: "$_id", RegistrationDate: "$date", TotalPurchasedLeads: { $size: "$ClientLeads" }, IncomeFromClient: { // This one is always undefined $multiply: [LEAD_PRICE / 2, "$TotalPurchasedLeads"] } } } ] I want to calculate IncomeFromClient by the mul of a

Merge $lookup result to existing array

ぐ巨炮叔叔 提交于 2020-01-21 18:04:30
问题 I'm new to mongo and I need your help. I have collection studijneProgramy . This is sample document: { "_id" : "dGFY", "garranti" : [ { "typ" : { "sk" : "garant", "en" : "Chairman of study board" }, "id" : "1025769" }, { "typ" : { "sk" : "predseda odborovej komisie", "en" : "Chairman of study board" }, "id" : "1025769" } ] } Next I have collection osoby . Example document: { "_id" : "1025769", "plneMeno" : "prof. RNDr. Peter Moczo, DrSc.", "priezvisko" : "Moczo", "meno" : "Peter",