Questions tagged [mongoose]

Mongoose is a MongoDB object modeling tool, or ODM (Object Document Mapper), written in JavaScript and designed to work in an asynchronous environment.

Mongoose is a MongoDB object modeling tool, or ODM (Object Document Mapper), written in JavaScript and designed to work in an asynchronous environment.

Documentation

Related links

To open a bug or feature request, visit: https://github.com/Automattic/mongoose/issues

For Mongoose, an embeddable web server written in C, use .

Installations

npm install --save mongoose
46587 questions
8
votes
5 answers

When using findOneAndUpdate(), how to leave fields as is if no value provided (instead of overwriting with null)?

The user has the option of updating any of a few values - however, if he only passes in { name: "new name" }, the email and password fields are also updated but to "null". How can I only update the fields that are actually provided in req.body,…
dan674
  • 1,848
  • 2
  • 15
  • 21
8
votes
2 answers

Mongoose, express- create schema with array of objects

I try to create mongoose schema containing array of objects. Schema looks like: const mapSchema = new Schema({ name: { type: String }, description: { type: String }, rate: { type: Number, default: 0 }, sumOfRates: { …
Krystian Fras
  • 91
  • 1
  • 2
  • 4
8
votes
3 answers

How do I update sub document with "save" function in mongoose?

My questions is bit naive but I could not find an answer after searching everywhere. I have a schema of user name: {type:String}, relations: [{ entity: mongoose.Schema.Types.ObjectId, year: {type:Number} }] I want to update the year of a…
Masade
  • 715
  • 1
  • 11
  • 29
8
votes
3 answers

How to mock mongoose?

I should run tests on node+express+mongoose+swagger app without mongodb, I need module to mock mongoose (only for tests). I tried mock-mongoose and mockgoose, but I had errors :( Maybe I failed.. or this modules can't help me I hope for your…
Jackson
  • 884
  • 2
  • 13
  • 22
8
votes
1 answer

mongodb connection timeout error with mongoose and nodejs

I desperately need some help.I am trying to upload large file(8 GB) to gridfs using mongoose and nodeJS. But as the file is very large it takes some time to upload. And after a while I get the following…
jony70
  • 225
  • 1
  • 2
  • 12
8
votes
1 answer

MongoDB nested lookup with 3 child levels

I need to retrieve the entire single object hierarchy from the database as a JSON. I'm trying aggregate for hours and can't solve how to do it with my data. So I have three collections: form { "_id" : "1", "name" : "My first form" } { "_id" : "2",…
Tiago Gouvêa
  • 15,036
  • 4
  • 75
  • 81
8
votes
2 answers

What's the best way to add a default admin user in Node.js/MongoDB app?

So I have an application developed in Nodejs and is using the Mongodb/Mongoose database. Beside the application there is an admin panel where administrators can manage all the data added from regular users. in the users schema I have…
ah92
  • 307
  • 5
  • 20
8
votes
5 answers

How to store object in mongoose schema?

In node.js I have three variables: var name = 'Peter'; var surname = 'Bloom'; var addresses = [ {street: 'W Division', city: 'Chicago'}, {street: 'Beekman', city: 'New York'}, {street: 'Florence', city: 'Los Angeles'}, ]; And…
fogen
  • 91
  • 1
  • 1
  • 3
8
votes
2 answers

Docker-compose mongoose

I'm new to Docker, and I'm trying the simplest of setups with docker-compose, but don't succeed to connect to Mongodb. My docker-compose.local.yaml file: version: "2" services: posts-api: build: dockerfile: Dockerfile.local …
html_programmer
  • 18,126
  • 18
  • 85
  • 158
8
votes
4 answers

How to resolve UnhandledPromiseRejectionWarning in mongoose?

I'm trying to fetch data using mongoose. So everytime i got to fetch the posts from the api -- localhost:3000/api/posts -- i get the foll error that i am unable to decypher. (node:12100) UnhandledPromiseRejectionWarning: Unhandled promise …
Nosail
  • 465
  • 2
  • 7
  • 19
8
votes
1 answer

How can i remove a specified field from all documents of a collection using mongoose?

I want to remove key "passwrod" from all documents from users collection using mongoose , is it possible to do it using $unset ? { "_id" : ObjectId("58ec890c91b2b612084fd827"), "username" : "zain", "passwrod" : 123, …
Haris KK
  • 267
  • 1
  • 4
  • 14
8
votes
3 answers

What to do with ENUM values in Mongoose?

Background I have a Mongoose schema that defines a set of possible values a given object can have. const mongoose = require("mongoose"); const COUNTRIES = ["ES", "PT", "US", "FR", "UK"]; const GENDERS = ["M", "F"]; const surveySchema = { …
Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266
8
votes
3 answers

How to trigger a function whenever a mongoose document is updated

I have a user model schema in mongoose which contains a list of friends and groups and stats info like so... var user = new Schema({ email: { type: String, required: true, unique: true }, password: { type: String, required: true, select: false…
CSharp
  • 1,396
  • 1
  • 18
  • 41
8
votes
2 answers

Populating in array of object ids

My schema:- var playlistSchema = new Schema({ name : {type:String,require:true}, videos : {type:[mongoose.Schema.Types.ObjectId],ref: 'Video'}, },{collection:'playlist'}) var Playlist = mongoose.model('Playlist',playlistSchema); I have…
lal rishav
  • 549
  • 3
  • 5
  • 17
8
votes
1 answer

TypeError: next is not a function

I'm running a Node.js-server and trying to test this Rest API that I made with Express. It's linked up to MongoDB using Mongoose. I'm testing the individual routes using Postman and I get an error when trying to send a PUT-request to this route: //…
SudokuNinja
  • 419
  • 1
  • 6
  • 16