aggregation-framework

MongoDB count by referenced document property

让人想犯罪 __ 提交于 2021-02-19 08:26:22
问题 db.foos { bar: ObjectId('123') } db.bars { _id: ObjectId('123') type: 'wine' } How can I in the simplest way find the number of foo-documents that refers to a bar-document of type 'wine'? Hopefully one that scales to perform fairly well even if the collections should contain a very large number of documents. 回答1: Try this aggregation framework query: db.foos.aggregate([ {$lookup: { from: "bars", localField: "_id", foreignField: "_id", as: "docs" } }, {$unwind: "$docs"}, {$match: {"docs.type":

Does MongoDB's $in clause has any max limit in number of arguments

雨燕双飞 提交于 2021-02-19 02:57:33
问题 When using MongoDB's $in clause with Aggregate , Does That has any max limit in number of arguments ? for example Model.aggregate( [ { $match : { '_Id' : { $in : ids } } } , { $group : { _id : '$roomId' , maxdate: { $max: "$date"}, } }, {$sort: { maxdate: -1} }, {$skip: skip}, {$limit: limitNum } ] In ids array , how many ids i can pass ? Currently i am not facing any issue with ids length till 50,000 ... but for safe side wanted to know the max limit. I have tried to search on Mongo doc ,

How to concatenate string results from multiple MongoDB records into a single result in MongoDB?

删除回忆录丶 提交于 2021-02-15 07:12:16
问题 Assuming the following records: { text: "foo"}, { text: "bar"} How can I get a result such as: results: "foo bar" The closest I am getting to it is to use $addToSet but this just creates an array with the results instead of a single string which is what I really want. results: [ "foo", "bar" ] Using Mongo 3.4 回答1: Use $group to get an array from all the documents and then $reduce with $concat to get one string: db.col.aggregate([ { $group: { _id: null, text: { $push: "$text" } } }, { $project

How to concatenate string results from multiple MongoDB records into a single result in MongoDB?

我怕爱的太早我们不能终老 提交于 2021-02-15 07:10:22
问题 Assuming the following records: { text: "foo"}, { text: "bar"} How can I get a result such as: results: "foo bar" The closest I am getting to it is to use $addToSet but this just creates an array with the results instead of a single string which is what I really want. results: [ "foo", "bar" ] Using Mongo 3.4 回答1: Use $group to get an array from all the documents and then $reduce with $concat to get one string: db.col.aggregate([ { $group: { _id: null, text: { $push: "$text" } } }, { $project

Filter Out duplicate arrays and return the unique array in mongodb aggregation

自闭症网瘾萝莉.ら 提交于 2021-02-11 16:55:07
问题 I have come a long way in structuring into the following mongodb data collection, but i couldn't finish the aggregation stage, { "test": [ { "_id": "60014aee808bc5033b45c222", "name": "a rogram", "companyName": "company NAme", "website": "https://www.example.comn", "loginUrl": "https://www.example.comn", "description": null, "createdBy": "5fe5cbcdb9ac0f001dccfadf", "createdAt": "2021-01-15T07:57:34.499Z", "updatedAt": "2021-01-15T13:09:09.417Z", "__v": 0, "address": null, "affiliatePlatform":

Filter Out duplicate arrays and return the unique array in mongodb aggregation

会有一股神秘感。 提交于 2021-02-11 16:54:15
问题 I have come a long way in structuring into the following mongodb data collection, but i couldn't finish the aggregation stage, { "test": [ { "_id": "60014aee808bc5033b45c222", "name": "a rogram", "companyName": "company NAme", "website": "https://www.example.comn", "loginUrl": "https://www.example.comn", "description": null, "createdBy": "5fe5cbcdb9ac0f001dccfadf", "createdAt": "2021-01-15T07:57:34.499Z", "updatedAt": "2021-01-15T13:09:09.417Z", "__v": 0, "address": null, "affiliatePlatform":

How to add auto incrementing field in MongoDB aggregate?

╄→尐↘猪︶ㄣ 提交于 2021-02-11 15:42:32
问题 I want to add an additional key number which holds a value of the auto-incrementing number. How can I achieve this? Below is the query. Is it possible to achieve this result without doing unwind and group? [ { "$match":{ "iContestId":"5e4118161a7b5765ee9b40ef" } }, { "$project":{ "_id":"$_id", "nPointsEarned":"$nPointsEarned", "iContestId":"$iContestId", "iUserId":"$iUserId", "ranking":"$ranking", "sFullName":"$sFullName", "sProfilePicture":"$sImage", "nAutoIncrement": { "$count": { "$sum": 1

How to add auto incrementing field in MongoDB aggregate?

佐手、 提交于 2021-02-11 15:41:51
问题 I want to add an additional key number which holds a value of the auto-incrementing number. How can I achieve this? Below is the query. Is it possible to achieve this result without doing unwind and group? [ { "$match":{ "iContestId":"5e4118161a7b5765ee9b40ef" } }, { "$project":{ "_id":"$_id", "nPointsEarned":"$nPointsEarned", "iContestId":"$iContestId", "iUserId":"$iUserId", "ranking":"$ranking", "sFullName":"$sFullName", "sProfilePicture":"$sImage", "nAutoIncrement": { "$count": { "$sum": 1

How to change elements of an array field to values of a dict with a single attribute in MongoDB

寵の児 提交于 2021-02-11 09:12:23
问题 I need a way to change the elements of an array field to values of a dict with a single attribute. I don't have to write the result back into my table. I just have to read it that way. My table has rows like this : {a: 1, b:[ {...}, ..., {...} ], c: 2} I need a query that returns each row rewritten like this : {a: 1, b: [ {foo: { ... }}, ..., {foo: {...}} ], c: 2} In other words, each element of b becomes a dict with a single attribute, foo . This feels like a job for $project or $replaceRoot

How to change elements of an array field to values of a dict with a single attribute in MongoDB

荒凉一梦 提交于 2021-02-11 09:11:09
问题 I need a way to change the elements of an array field to values of a dict with a single attribute. I don't have to write the result back into my table. I just have to read it that way. My table has rows like this : {a: 1, b:[ {...}, ..., {...} ], c: 2} I need a query that returns each row rewritten like this : {a: 1, b: [ {foo: { ... }}, ..., {foo: {...}} ], c: 2} In other words, each element of b becomes a dict with a single attribute, foo . This feels like a job for $project or $replaceRoot