aggregation-framework

mongoDB: Show MinuteBucket in Time value as ending time for the interval

时光怂恿深爱的人放手 提交于 2020-04-16 09:18:47
问题 I have created Mongo PlayGround Currently dateHour field is dividing time by 15min interval. For example - if `"updatedAt": ISODate("2020-03-20T18:16:50.000Z")` it shows as : "dateHour": ISODate("2020-03-20T18:15:00Z"), so First 15min bucket is represented as 18:00:000z (this covers minutes from 1 to 15) second 15min bucket is represented as 18:15:000z (this covers minutes from 15 to 30) and so on.... Expected - First 15min bucket should be displayed as 18:15:000z (this covers minutes from 1

How to turn an array of objects into an object in mongodb aggregation

夙愿已清 提交于 2020-04-16 03:32:47
问题 I had asked this question here and I got a satisfactory answer. But I have a new problem that I am unable to solve. The output I am getting from the query is an array of objects. How can I turn that array into an object? I already know how to do it in plain javascript but how can I do it in a mongodb aggregation query? You can assume that you have the data below in the aggregation query. [ { "choiceA": [ {"_id": ObjectId("..."), "voter": "Juzi", "choice": 0, "pollId": 100 }, {"_id": ObjectId(

How to turn an array of objects into an object in mongodb aggregation

我怕爱的太早我们不能终老 提交于 2020-04-16 03:32:05
问题 I had asked this question here and I got a satisfactory answer. But I have a new problem that I am unable to solve. The output I am getting from the query is an array of objects. How can I turn that array into an object? I already know how to do it in plain javascript but how can I do it in a mongodb aggregation query? You can assume that you have the data below in the aggregation query. [ { "choiceA": [ {"_id": ObjectId("..."), "voter": "Juzi", "choice": 0, "pollId": 100 }, {"_id": ObjectId(

how to create an array on the output of a response using aggregate in Mongodb

感情迁移 提交于 2020-04-16 02:41:30
问题 I have in my collection a list of objects with this structure: [ { "country": "colombia", "city":"medellin", "calification": [ { "_id": 1, "stars": 5 }, { "_id": 2, "stars": 3 } ] }, { "country": "colombia", "city":"manizales", "calification": [ { "_id": 1, "stars": 5 }, { "_id": 2, "stars": 5 } ] }, { "country": "argentina", "city":"buenos aires", "calification": [ { "_id": 1, "stars": 5 }, ] }, { "country": "perú", "city":"cusco", "calification": [ { "_id": 3, "stars": 3 }, ] } ] I am

how to create an array on the output of a response using aggregate in Mongodb

我是研究僧i 提交于 2020-04-16 02:41:08
问题 I have in my collection a list of objects with this structure: [ { "country": "colombia", "city":"medellin", "calification": [ { "_id": 1, "stars": 5 }, { "_id": 2, "stars": 3 } ] }, { "country": "colombia", "city":"manizales", "calification": [ { "_id": 1, "stars": 5 }, { "_id": 2, "stars": 5 } ] }, { "country": "argentina", "city":"buenos aires", "calification": [ { "_id": 1, "stars": 5 }, ] }, { "country": "perú", "city":"cusco", "calification": [ { "_id": 3, "stars": 3 }, ] } ] I am

How can I get count of the Documents and filter them in efficient way? (mongoose)

大城市里の小女人 提交于 2020-04-16 02:35:51
问题 I'm implementing search function that is simply find document in mongoDB. I want to .skip(x) and .limit(x) on result to simulate paging result, but can I get total count of document (before skip and limit) and get filtered result at once? Code that produce Expected Output : db.Datas.find({ type: "Unknown" }) .then((result) => { let count = result.length; db.Datas.find({ type: "Unknown" }) .sort({ createdAt: -1 }) .skip((req.query.page - 1) * 10) .limit(10) .then((res) => { res.json({ count:

How can I manipulate a date field in an aggregation pipeline?

筅森魡賤 提交于 2020-04-07 10:30:45
问题 I'm trying to set the time from a Date field to the start of the day function getDate(date){ return new Date(date.getYear(), date.getMonth(), date.getDate(), 0,0,0); } ... {"$project" : { "_id" : getDate("$dt"), ... If I send "$dt" I get TypeError: date.getYear is not a function as I'm passing a string, If remove the quotation marks, I get Error: "$dt is not defined" , but if I set "$dt" as value for "_id" I get a correct ISO Date. So how do I pass the date object to the function? 回答1:

How can I manipulate a date field in an aggregation pipeline?

让人想犯罪 __ 提交于 2020-04-07 10:30:36
问题 I'm trying to set the time from a Date field to the start of the day function getDate(date){ return new Date(date.getYear(), date.getMonth(), date.getDate(), 0,0,0); } ... {"$project" : { "_id" : getDate("$dt"), ... If I send "$dt" I get TypeError: date.getYear is not a function as I'm passing a string, If remove the quotation marks, I get Error: "$dt is not defined" , but if I set "$dt" as value for "_id" I get a correct ISO Date. So how do I pass the date object to the function? 回答1:

How to match a string consisting special character like hyphen and spaces

我的未来我决定 提交于 2020-04-07 08:13:10
问题 I have a string as: word : 'A-Scan Ultrasonic' How to match this with : 'A Scan Ultrasonic' I have tried my luck as {word:{$regex:".*A Scan Ultrasonic.*",$options: 'i'}} But this doesn't fetch any result May I know how this can be matched , any help is appreciated , TIA 回答1: It's actually no mongo thing, you should use proper regex. Try with this: { name: { $regex: /.*A(-|\s)Scan(-|\s)Ultrasonic.*/}} 来源: https://stackoverflow.com/questions/60810426/how-to-match-a-string-consisting-special

How to match a string consisting special character like hyphen and spaces

隐身守侯 提交于 2020-04-07 08:12:27
问题 I have a string as: word : 'A-Scan Ultrasonic' How to match this with : 'A Scan Ultrasonic' I have tried my luck as {word:{$regex:".*A Scan Ultrasonic.*",$options: 'i'}} But this doesn't fetch any result May I know how this can be matched , any help is appreciated , TIA 回答1: It's actually no mongo thing, you should use proper regex. Try with this: { name: { $regex: /.*A(-|\s)Scan(-|\s)Ultrasonic.*/}} 来源: https://stackoverflow.com/questions/60810426/how-to-match-a-string-consisting-special