mongoose

Mongoose .pre('save') does not trigger

戏子无情 提交于 2021-01-21 12:10:29
问题 I have the following model for mongoose.model('quotes') : var mongoose = require('mongoose'); var Schema = mongoose.Schema; var quotesSchema = new Schema({ created: { type: String, default: moment().format() }, type: { type: Number, default: 0 }, number: { type: Number, required: true }, title: { type: String, required: true, trim: true}, background: { type: String, required: true }, points: { type: Number, default: 1 }, status: { type: Number, default: 0 }, owner: { type: String, default:

Convert multiple array into one array in MongoDB, also remove the None values from Arrays

大城市里の小女人 提交于 2021-01-21 11:32:45
问题 Mongo DB Array Structure: { "time" :[ [2018-12-18T20:16:28.800Z , 2018-12-18T20:16:28.800Z, 2016-11-14T10:16:32.700Z], [2016-11-14T10:16:32.700Z, 2017-09-17T11:16:54.500Z, 2018-12-18T20:16:28.800Z], [2017-09-17T11:16:54.500Z, 2018-12-18T20:16:28.800Z, 2016-11-14T10:16:32.700Z] ], "bar": [ [2,5,9,8], [6,3,7,None], [7,8,None] ] } Expected output with One array without None values: "time" :[ 2018-12-18T20:16:28.800Z, 2018-12-18T20:16:28.800Z, 2016-11-14T10:16:32.700Z, 2016-11-14T10:16:32.700Z,

Convert multiple array into one array in MongoDB, also remove the None values from Arrays

蹲街弑〆低调 提交于 2021-01-21 11:31:08
问题 Mongo DB Array Structure: { "time" :[ [2018-12-18T20:16:28.800Z , 2018-12-18T20:16:28.800Z, 2016-11-14T10:16:32.700Z], [2016-11-14T10:16:32.700Z, 2017-09-17T11:16:54.500Z, 2018-12-18T20:16:28.800Z], [2017-09-17T11:16:54.500Z, 2018-12-18T20:16:28.800Z, 2016-11-14T10:16:32.700Z] ], "bar": [ [2,5,9,8], [6,3,7,None], [7,8,None] ] } Expected output with One array without None values: "time" :[ 2018-12-18T20:16:28.800Z, 2018-12-18T20:16:28.800Z, 2016-11-14T10:16:32.700Z, 2016-11-14T10:16:32.700Z,

MongoDB: I want to store array object in collection

邮差的信 提交于 2021-01-21 11:15:47
问题 I get array object from front side, and want to save it in collection using NodeJS, Mongodb. My object is: routerData={"User-Name": {"type":"string","value":["\u0000\u0000\u0000\u0000"]}, "NAS-IP-Address": {"type":"ipaddr","value":["10.1.0.1"]} }, My collection schema is: var model = new Schema({ routerData:{ "User-Name": { "type": String, "value": [] }, "NAS-IP-Address": { "type": String, "value": [] }, }, }); I am trying with this code: var obj = new objModel(req.body); obj.routerData = req

MongoDB: I want to store array object in collection

佐手、 提交于 2021-01-21 11:14:35
问题 I get array object from front side, and want to save it in collection using NodeJS, Mongodb. My object is: routerData={"User-Name": {"type":"string","value":["\u0000\u0000\u0000\u0000"]}, "NAS-IP-Address": {"type":"ipaddr","value":["10.1.0.1"]} }, My collection schema is: var model = new Schema({ routerData:{ "User-Name": { "type": String, "value": [] }, "NAS-IP-Address": { "type": String, "value": [] }, }, }); I am trying with this code: var obj = new objModel(req.body); obj.routerData = req

MongoDB $lookup pipeline match by _id not working

依然范特西╮ 提交于 2021-01-21 09:17:46
问题 i'm trying to do a $lookup in a collection and add some data to my documents. The problem is that when i try matching my $lookup pipeline by _id it returns an empty array. Here is my code: Schedule.aggregate([{ // My Schedule schema $match: { store: req.query.store, "customer.id": req.query.user } }, { $skip: +req.query.skip }, { $limit: +req.query.limit }, { $lookup: { from: Employee.collection.name, // "employee" schema, let: { id: "$employee.id" // employee _id from the "schedule"

MongoDB $lookup pipeline match by _id not working

巧了我就是萌 提交于 2021-01-21 09:17:11
问题 i'm trying to do a $lookup in a collection and add some data to my documents. The problem is that when i try matching my $lookup pipeline by _id it returns an empty array. Here is my code: Schedule.aggregate([{ // My Schedule schema $match: { store: req.query.store, "customer.id": req.query.user } }, { $skip: +req.query.skip }, { $limit: +req.query.limit }, { $lookup: { from: Employee.collection.name, // "employee" schema, let: { id: "$employee.id" // employee _id from the "schedule"

mongoose Date comparing without time and Group by createdAt and staffId with Weekly, monthly and Yearly total of staff count by aggregation?

妖精的绣舞 提交于 2021-01-20 10:53:17
问题 Might be question is look like duplicate but apologize for it. I want to aggregate the weekly, Monthly, Yearly result basis of createdAt property without time and staffId . Model is like below: { "_id" : ObjectId("5f351f3d9d90b1281c44c5dp"), "staffId" : 12345, "category" : "trend", "page_route" : "http://example.com/rer", "expireAt" : ISODate("2020-08-13T11:08:45.196Z"), "createdAt" : ISODate("2020-08-13T11:08:45.199Z"), "updatedAt" : ISODate("2020-08-13T11:08:45.199Z"), "__v" : 0 } { "_id" :

mongoose Date comparing without time and Group by createdAt and staffId with Weekly, monthly and Yearly total of staff count by aggregation?

拜拜、爱过 提交于 2021-01-20 10:49:52
问题 Might be question is look like duplicate but apologize for it. I want to aggregate the weekly, Monthly, Yearly result basis of createdAt property without time and staffId . Model is like below: { "_id" : ObjectId("5f351f3d9d90b1281c44c5dp"), "staffId" : 12345, "category" : "trend", "page_route" : "http://example.com/rer", "expireAt" : ISODate("2020-08-13T11:08:45.196Z"), "createdAt" : ISODate("2020-08-13T11:08:45.199Z"), "updatedAt" : ISODate("2020-08-13T11:08:45.199Z"), "__v" : 0 } { "_id" :

Mongoose find documents if array contains a value

纵然是瞬间 提交于 2021-01-07 06:24:36
问题 so i have this schema const Document = new mongoose.Schema({ _id:{ type:Number }, creationDate:{ type:Date, default:Date.now(), }, title:String, status:{ type:String, default:status.PENDING }, description: String, category:[{ type:mongoose.Schema.Types.ObjectId, ref:'Categories', }], }) how do i find documents that their category array contains an id given ? i mean like a query to get all documents using category id 回答1: There are some ways to achieve this. First one is by $elemMatch operator