Skip to content
Advertisement

Tag: mongoose

Add field not in schema with mongoose

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

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

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

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

Advertisement