I am trying to add a new field to a document, but this isn’t working: Creating my UserModel prototype: Then calling it This successfully updates any field as long as it exists, but it won’t add any new one. Answer You can add and remove fields in schema using option { strict: false } option: strict The strict option, (enabled
Tag: mongoose
UnhandledPromiseRejectionWarning: ValidationError
I’m running into a trouble since a few days. I’m learning the MEAN stack, but during creation of a user on mongo using mongoose schema, I have this problem : (node:93337) UnhandledPromiseRejectionWarning: ValidationError: User validation failed: username: Path username is required., password: Path password is required., email: Path email is required. Here’s my code : The server part : …
How to force Mongoose to ignore __v if passed?
Working with mongoose and Express for a basic data endpoint, and I’m having trouble with the Update portion of the CRUD operations. Testing the Update path works in Postman, but when I try from my angular app, it returns this: MongoError: Updating the path ‘__v’ would create a conflict at ‘__v’ at C:Usersrutherfordc.AADocumentsGitHubtechInventorynode_modulesmongoosenode_modulesmongodb-corelibconne ctionpool.js:595:61 at authenticateStragglers (C:Usersrutherfordc.AADocumentsGitHubtechInventorynode_modulesmongoosenode_module smongodb-corelibconnectionpool.js:513:16) at Connection.messageHandler
only return document _id on mongoose .find()
I update 100’s of documents every second by pushing new data to an array in its document. To get the document of which I am going to add data to, I use the mongoose .find().limit(1) function, and return the whole document. It works fine. To help with some memory and cpu issues I have, I was wondering how I could
Returning specific fields with mongoose
I’m trying to accomplish something really easy but still manage to fail. What I am trying to do is when I get a get request on my server I want to return all documents BUT just the specific fields populated. My schema goes as follows What I want is to return only client.phone and client.email plus orderdetails.status but still retain
How to make Mongoose not insert empty array or object fields into a document
Let’s say we have a Mongoose schema in our Node.js project: And let’s we have an according object: Now to save this data into MongoDB we can use this code: Ok, while it’s all cool. But if data does not contain field_3 (array field, and the same will be for an object field) Mongoose will anyway add this field into
mongoose save vs insert vs create
What are different ways to insert a document(record) into MongoDB using Mongoose? My current attempt: Any idea why insert and save doesn’t work in my case? I tried create, it inserted 2 document instead of 1. That’s strange. Answer The .save() is an instance method of the model, while the .create() is called directly from the Model as a method
Multiple schema references in single schema array – mongoose
Can you populate an array in a mongoose schema with references to a few different schema options? To clarify the question a bit, say I have the following schemas: Can I populate the guns array with a bunch of ak47 OR m16? Can I put BOTH in the same guns array? Or does it require a populate ref in the
chain promises in javascript
I’ve created many promises like that, in order to create object in my database. At the end, I want call all my promises in the order I want. (because somes object depend of other, so I need to keep that order) So I expect to see : Unfortunately, this messages are shuffled and I don’t understand what. Thanks Answer Looks
mongoose “Find” with multiple conditions
I am trying to get data from my mongoDB database by using mongoose filters. The scenario is that each user object in the database has certain fields like “Region” or “Sector”. Currently I am getting all the users that contain the keyword “region” in there object like so: How can put some conditions in mongoose that it return users that