mongoose

I want my pre('save') mongoose function to operate only once

半城伤御伤魂 提交于 2021-02-05 10:29:28
问题 I do not know if the exact request in title is possible, but if not; i would really appreciate an alternate solution. I have this pre save method of mongoose ownerSchema.pre("save", function(next) { const owner = this; bcrypt.genSalt(10, function(err, salt) { bcrypt.hash(owner.password, salt, function(err, hash) { // Store hash in your password DB. owner.password = hash; next(); }); }); }); When i save new user(owner) a hash is created successfully and all is good> The problem occurs when i

I want my pre('save') mongoose function to operate only once

落花浮王杯 提交于 2021-02-05 10:28:17
问题 I do not know if the exact request in title is possible, but if not; i would really appreciate an alternate solution. I have this pre save method of mongoose ownerSchema.pre("save", function(next) { const owner = this; bcrypt.genSalt(10, function(err, salt) { bcrypt.hash(owner.password, salt, function(err, hash) { // Store hash in your password DB. owner.password = hash; next(); }); }); }); When i save new user(owner) a hash is created successfully and all is good> The problem occurs when i

MongoDB Mongoose select documents between a date range

做~自己de王妃 提交于 2021-02-05 09:32:29
问题 I've a collection called events . Sample document is like this. I want to select documents with in date range of '2019-01-11' to '2019-11-27' . What I've done is this. But this seems not working. How do I achieve this using MongoDB Mongoose ? $match: { createdAt: { $gte: { $dayOfYear: '2019-01-11' }, $lte: { $dayOfYear: '2019-11-27' } } } 回答1: Use the Date() object instead. I am not sure if this affects the timezone as well. db.collection.find({ "createdAt": { "$gte": new Date('2019, 1, 11'),

Update or append to a subcollection in mongo

狂风中的少年 提交于 2021-02-05 08:17:26
问题 I have a collection containing a subcollection. In one request, I would like to update a record in the subcollection or append to it if a match doesn't exist. For a bonus point I would also like this update to be a merge rather than an overwrite. A crude example: // Schema { subColl: [ { name: String, value: Number, other: Number, }, ]; } // Existing record { _id : 123, subColl: [ {name: 'John', value: 10, other: 20} ] } // example const update = { _id: 123, name: 'John', other: 1000 }; const

MongoDB - Update field in an array object based on nested array's field value

喜夏-厌秋 提交于 2021-02-05 08:13:28
问题 I am trying to update a field inside array of objects, where field in nested array is equal to a value. My goal here is to set the picture field a new url, where value field in valueList is oldRed Product schema: { variations: [{ id: 1, picture: 'https://example.picture.com', valueList: [{ name: 'color', value: 'oldRed' }, { name: 'size', value: 'M' }] }, { id: 2, picture: 'https://example.picture.com', valueList: [{ name: 'color', value: 'black' }, { name: 'size', value: 'M' }] }] } The

Connect to mongodb with mongoose both in kubernetes

我的梦境 提交于 2021-02-05 08:09:39
问题 I have a microservice which I developed and tested using docker-compose. Now I would like to deploy it to kubernetes. Part of my docker-compose file looks like this: tasksdb: container_name: tasks-db image: mongo:4.4.1 restart: always ports: - '6004:27017' volumes: - ./tasks_service/tasks_db:/data/db networks: - backend tasks-service: container_name: tasks-service build: ./tasks_service restart: always ports: - "5004:3000" volumes: - ./tasks_service/logs:/usr/src/app/logs - ./tasks_service

Recursive auto increment function in not resolving

泪湿孤枕 提交于 2021-02-05 08:08:58
问题 Long story short I am using this function to get an unique username for a user, but there's a possibility that it already exists - in this case it should run again until there's no user with it. The problem is that when called again from inside, the function doesn't resolve. The function itself is working (it's updating the document in mongodb) but I can't get the sequence number. What am I doing wrong? Any advice is appreciated. const getNextUsername = () => { return new Promise((res, rej) =

Mongoose - find(): object inside search options is not working

耗尽温柔 提交于 2021-02-05 07:52:06
问题 I have a mongoose Schema that looks like this: var mySchema = new mongoose.Schema({ ... metadata: { isDeleted: { type: Boolean, default: false }, ... } }); I want to get the list of elements in my mongodb database applying a filter, so I have the following object: var searchOptions = { metadata: { isDeleted: false } }; that always needs to have that metadata.isDeleted value set to false , appart from other parameters that will be added later, before executing the query: var objQuery = myModel

Update array inside of Mongo document doesn't work

自作多情 提交于 2021-02-05 07:32:42
问题 I have a user's collection, each user has a subscription array of objects. I want to add another subscription to that array. I'm getting the new subscription, updating it with findByIdAndUpdate , but it doesn't add the new subscription, however it shows that the document was updated. I tried several approaches but nothing worked well. Here is the last approach: ... const { user_id } = req.params; const subscription = req.body; // Getting subscription const user = await UserModel.findById(user

Update array inside of Mongo document doesn't work

非 Y 不嫁゛ 提交于 2021-02-05 07:32:08
问题 I have a user's collection, each user has a subscription array of objects. I want to add another subscription to that array. I'm getting the new subscription, updating it with findByIdAndUpdate , but it doesn't add the new subscription, however it shows that the document was updated. I tried several approaches but nothing worked well. Here is the last approach: ... const { user_id } = req.params; const subscription = req.body; // Getting subscription const user = await UserModel.findById(user