mongodb-query

Mongoose: how to use aggregate and find together

若如初见. 提交于 2020-05-09 17:54:27
问题 How can I use aggregate and find together in Mongoose? i.e I have the following schema: const schema = new Mongoose.Schema({ created: { type: Date, default: Date.now() }, name: { type: String, default: 'development' } followers: [{ type: Mongoose.Schema.ObjectId, ref: 'Users'}] ... }) export default Mongoose.model('Locations', schema) How can I query the users with only the fields name and followers_count . followers_count : the length of followers . There, I know we can use select to get

MongoDB: how to parse date in 3.6 mongoDb version?

微笑、不失礼 提交于 2020-05-09 11:07:56
问题 I have created Mongo Playground here Current output is showing result based on 15min time interval. (grouping updatedAt value by 15mins and shows avg for some field) Currently $dateToString and $dateFromString is using format to parse the date. I need to make it work for mongo version 3.6 (3.6 is not supporting format for $dateFromString) parsedDate: { $dateFromString: { dateString: "$_id.dateHour", format: "%Y-%m-%dT%H" } } If I remove format field from both $dateToString and $dateFromString

MongoDB: how to parse date in 3.6 mongoDb version?

时间秒杀一切 提交于 2020-05-09 11:06:34
问题 I have created Mongo Playground here Current output is showing result based on 15min time interval. (grouping updatedAt value by 15mins and shows avg for some field) Currently $dateToString and $dateFromString is using format to parse the date. I need to make it work for mongo version 3.6 (3.6 is not supporting format for $dateFromString) parsedDate: { $dateFromString: { dateString: "$_id.dateHour", format: "%Y-%m-%dT%H" } } If I remove format field from both $dateToString and $dateFromString

MongoDB: how to parse date in 3.6 mongoDb version?

蹲街弑〆低调 提交于 2020-05-09 11:06:30
问题 I have created Mongo Playground here Current output is showing result based on 15min time interval. (grouping updatedAt value by 15mins and shows avg for some field) Currently $dateToString and $dateFromString is using format to parse the date. I need to make it work for mongo version 3.6 (3.6 is not supporting format for $dateFromString) parsedDate: { $dateFromString: { dateString: "$_id.dateHour", format: "%Y-%m-%dT%H" } } If I remove format field from both $dateToString and $dateFromString

MongoDb: Convert date string to specific format in mongodb 3.6

浪子不回头ぞ 提交于 2020-05-09 07:19:43
问题 I need to parse date value to specific format without using format field in dateFromString operator. Mongo Playground Current situation : in Mongodb 4.0 if I format dateString using below it code it give me mentioned output. parsedDate: { $dateFromString: { dateString: "$dateS", format: format: "%Y-%m-%dT%H" } } Output: "parsedDate": ISODate("2020-01-16T08:00:00Z") I cannot use format field in 3.6 since its not supported. How do I convert my date to format: "%Y-%m-%dT%H" in 3.6? 回答1: If I get

MongoDb: Convert date string to specific format in mongodb 3.6

青春壹個敷衍的年華 提交于 2020-05-09 07:17:08
问题 I need to parse date value to specific format without using format field in dateFromString operator. Mongo Playground Current situation : in Mongodb 4.0 if I format dateString using below it code it give me mentioned output. parsedDate: { $dateFromString: { dateString: "$dateS", format: format: "%Y-%m-%dT%H" } } Output: "parsedDate": ISODate("2020-01-16T08:00:00Z") I cannot use format field in 3.6 since its not supported. How do I convert my date to format: "%Y-%m-%dT%H" in 3.6? 回答1: If I get

Decrement value in mongodb

末鹿安然 提交于 2020-05-09 06:33:48
问题 I am creating a shopping application. Each user have wallet. Structure is like : { "userName" : "Gandalf the Grey", "wallet" : 100, "orderHistory" : [] } Say this user buys something that costs 50 units. Is there a better way Instead of fetching its wallet value with findOne, then making substraction and updating new wallet value? Right now, I am making it with 2 different operations that looks like dbo.collection('users').findOne({'userName': controller.userName}) .then(function(doc) {

Group by in mongoDB with property construction

老子叫甜甜 提交于 2020-05-09 06:26:27
问题 I have a collection named Accounts like below data /* 1 */ { "_id" : ObjectId("5e93fea52f804ab99b54e4a2"), "account" : "first", "connect" : "Always", "desc" : "first account feature first phase", "status" : true } /* 2 */ { "_id" : ObjectId("5e93fea32c804ab99b12e4d1"), "account" : "second", "connect" : "Sometimes", "desc" : "second account feature first phase", "status" : true } /* 3 */ { "_id" : ObjectId("5e93fea52f804ab99b55a7b1"), "account" : "first", "connect" : "Sometimes", "desc" :

Group by in mongoDB with property construction

僤鯓⒐⒋嵵緔 提交于 2020-05-09 06:26:13
问题 I have a collection named Accounts like below data /* 1 */ { "_id" : ObjectId("5e93fea52f804ab99b54e4a2"), "account" : "first", "connect" : "Always", "desc" : "first account feature first phase", "status" : true } /* 2 */ { "_id" : ObjectId("5e93fea32c804ab99b12e4d1"), "account" : "second", "connect" : "Sometimes", "desc" : "second account feature first phase", "status" : true } /* 3 */ { "_id" : ObjectId("5e93fea52f804ab99b55a7b1"), "account" : "first", "connect" : "Sometimes", "desc" :

Group by in mongoDB with property construction

天涯浪子 提交于 2020-05-09 06:26:00
问题 I have a collection named Accounts like below data /* 1 */ { "_id" : ObjectId("5e93fea52f804ab99b54e4a2"), "account" : "first", "connect" : "Always", "desc" : "first account feature first phase", "status" : true } /* 2 */ { "_id" : ObjectId("5e93fea32c804ab99b12e4d1"), "account" : "second", "connect" : "Sometimes", "desc" : "second account feature first phase", "status" : true } /* 3 */ { "_id" : ObjectId("5e93fea52f804ab99b55a7b1"), "account" : "first", "connect" : "Sometimes", "desc" :