mongoose

Can not add additional element to (mongoose) object

空扰寡人 提交于 2021-01-29 09:16:06
问题 I have a nodejs express application with an api to return data from a mongodb database. This is my mongoose model: const bookingSchema = new mongoose.Schema({ timestamp: { type: Date, default: Date.now, required: true }, tags: { type: [String], required: true }, amount: { type: Number, required: true }, type: { type: String, required: true, enum: ['expense', 'income'] } }) When I'm calling the api with the path /api/bookings/listbymonth/2019/1 this function inside the backend gets called:

MongoDB: how to update only one element in the document?

谁说胖子不能爱 提交于 2021-01-29 09:14:23
问题 I want to updates only one element in the document to use put request I tried PUT localhost:3000/api/memo/5b8e382d61984255d879f872 { text: "updated!" { but, it updated like this { text: "updated!", imgUrl: null, tag: null, ... } Is there a way to updates one element in document? await Memo.where('_id') .equals(req.params.id) .updateOne({ imgUrl: req.body.imgUrl, text: req.body.text, tag: req.body.tag, user: req.body.user, loc: req.body.loc }) 回答1: You can use the following solution to update

How can I make a number id pattern 1, 2, 3, 4 in mongoose

佐手、 提交于 2021-01-29 09:00:25
问题 How do I make a pattern like this? I can't code that because i'm new to this. This is my code array = 1,2,3,4,5 const note = new notedModel ({ _id: array, note: args[1], }); Also using other modules if you ask. 回答1: I hope it could be helpful const array = [1,2,3,4,5]; const notes = array.map(element => new notedModel({ _id: element, note: args[1], // or maybe args[element] if you need to select arg by array's element })); P.S. You could read answers here to be careful with ids: How do you

How to use MongoDB $ne on nested object property

家住魔仙堡 提交于 2021-01-29 08:55:38
问题 I have a node API which connects to a mongoDB through mongoose. I am creating an advanced results middleware that enabled selecting, filtering, sorting, pagination etc. based on a Brad Traversy course Node.js API Masterclass With Express & MongoDB. This is all good. I am adapting the code from the course to be able to use the $ne (not equal) operator and I want to be able to get a model that is not equal to a nested property (user id) of the model. I am using this for an explore feature to

merge multiple documents into one document with both document fields in MongoDB

此生再无相见时 提交于 2021-01-29 08:48:37
问题 I have multiple documents In MobgoDB How to do to the group on "abc" and "xyz" and get one document. Please see the "Output Document". need to do the union with ( Document 1 U Document 2 ) and (Document 1 U Document 3) . U= Union Document 1 { "data": { "Inside_data": { "project": { "abc": { "alpha": 4, "beta" : 45 }, "xyz": { "alpha": 214, "beta" : 431 } } } } } Document 2 "Deal": { "name": "abc", "url" : "www.abc.com, "email": [ "abc@gmail.com"], "total": 2 } Document 3 "Deal": { "name":

Pagination with mongoose and nestjs

早过忘川 提交于 2021-01-29 08:08:01
问题 Im trying to use mongoose paginate to paginate through an array of values. class subNotes { @Prop() Note: string; @Prop() Date: Date; } @Schema() class Travel extends Document { @Prop() Country: string; @Prop() Recommendation: string; @Prop() Level: number; @Prop() LevelDescr: string; @Prop() Date: Date; @Prop() Notes: [subNotes]; } export const TravelSchema = SchemaFactory.createForClass(Travel); TravelSchema.plugin(mongoosePaginate); So subnotes are updated weekly and will have lots of

Mysteriously Invalid Mongoose Query

╄→尐↘猪︶ㄣ 提交于 2021-01-29 07:35:46
问题 I'm getting stuck on a stubborn bug in my MEPN app. This pseudo-code is supposed to assemble a Mongoose query from the options submitted in a user form, and then search a collection using that query. var query = []; query.push("{name:"+req.body.name+"}"); query.push("{status:{$in:["+req.body.statusarray+"]}}"); query.push("{range:{$gte:"+req.body.min+",$lte:"+req.body.max+"}}"); Collection.find(query, function(error, cursor){ if(error) console.log("ERROR: "+error); else //do something })

Why does MongoDB not update unless I call “.then res.json(…)” after findOneAndUpdate?

守給你的承諾、 提交于 2021-01-29 07:03:21
问题 understanding questions here. I am doing a MERN setup website, and I was updating a field in my database like this: router.post("/updateLogApprovementStatus", (req, res) => { User.findOneAndUpdate( { _id: req.body.userId }, { $set: { log: req.body.newData } } ).then(user => res.json(user.log)); }); But after doing repeated calls to the api with correct data, my data field wasn't updating. However, when I do this: router.post("/updateLogApprovementStatus", (req, res) => { User.findOneAndUpdate

how to sort array of objects node.js and mongoose

孤街醉人 提交于 2021-01-29 05:59:17
问题 I have an array of objects that I want to sort by. .sort() at top level works like this according to documentation: Product.findById(productId) .sort({ somefield: -1 }) I have tried something like this: Product.findById(productId) .sort( { requests: { _id: -1 } } ) But I get the following error: TypeError: Invalid sort value: { requests: [object Object] } If I have the object ID's in this order in mongodb (compass) _id: 123456789 _id: 987654321 I want _id 987654321 show first in the list. I

How to update existing object inside an array using mongoose

依然范特西╮ 提交于 2021-01-29 05:49:41
问题 I'm trying to make a currency system with my discord.js bot and mongoose. Here is an example MongoDB document format: { guild: "2095843098435435435", wallets: [ { id: "2323232335354", amount: 10, }, { id: "24344343234454", amount: "i want to update this", }, ], }; As far as I know, the Array.prototype.push() function is for making a new object inside an array. But how can I update an existing object inside an array? My code: const find = await Account.findOne({ guild: message.guild.id, }); if