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
2 answers

InsertMany not working in mongodb

I'm fairly new to Mongoose and MongoDB itself and I'm trying to save a bunch of documents inserted via insertMany method but it is not saving the docs. Here is my code: Model: var mongoose = require('mongoose'); var Schema = mongoose.Schema; var…
Marrone
  • 487
  • 2
  • 7
  • 18
8
votes
3 answers

isModified and pre-save mongoose...Nodejs

Hi i want save with hashed password only if password is change, so i used isModified function in pre-save, but its always return false even i changed the password. The reason that i am trying to do this is because i dont want to change and save my…
Scott Kim
  • 233
  • 1
  • 4
  • 13
8
votes
1 answer

Get all elements with matching id in array of id

I am using Mongoose in my Express / React web application and I'm storing data in a Mongo Database. I store 'songs' in a songs collection and the user has an array containing the ids of the songs he listened to for example. Then to render what he's…
Yooooomi
  • 895
  • 1
  • 8
  • 20
8
votes
2 answers

MongoNetworkError: connection 0 to localhost:27017 closed

I can´t get this to work. I have opened mongo and mongod, and this is what I get when I write "node server.js" in git Bash or cmd: Running on server27017 Not connected to database MongoNetworkError: connection 0 to localhost:27017 closed and here…
helloRebecca
  • 107
  • 1
  • 5
8
votes
2 answers

How to handle Many to Many relationship in mongoDB?

I have a specific problem with many to many relationship implementations in MongoDB. I have collections of Songs and Artists(Millions document). Here the song can be sung by Many Artists and an artist can sing Many songs. So I followed the approach…
Abhishek Singh
  • 1,631
  • 1
  • 17
  • 31
8
votes
7 answers

Mongoose: ignore parameter in query if it is null

I have a simple REST API and I would like to implement filtering on a specific endpoint. Imagine I have an endpoint like this: localhost:5000/api/items?color="red" Which handles request like this: const items = await Items.find({color:…
JesterWest
  • 499
  • 1
  • 5
  • 13
8
votes
2 answers

Validate a mongodb query syntax programmatically

I have an API method where the user can pass in their own query. The field in the collection is simply ns, so the user might pass something like: v.search = function(query: Object){ // query => {ns:{$in:['foo','bar',baz]}} // valid! //…
user7898461
8
votes
2 answers

Mongoose - Cannot populate with sort on path notifications.from because it is a subproperty of a document array

I have a very simple mongo scheme I'm accessing with mongoose I can map the username and firstname to each notification's from field by using populate, the issue is I can't seem to get any sorting to work on the date field With this code I get an…
totalnoob
  • 2,521
  • 8
  • 35
  • 69
8
votes
1 answer

Mongoose save() doesn't return promise

Mongoose's documentations affirms that method save returns a promise. I would like to save a user model in my database using this method. I do this like that : save (user) { user.save((err, user) => { if (err) { return…
user9022931
8
votes
2 answers

Mongodb Updating the path would create a conflict

Im trying to insert new competitor if not exist, else only updating it, but im getting the following error: (node:4628) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: Updating the path 'competitors'…
maria
  • 207
  • 5
  • 22
  • 56
8
votes
2 answers

MongoDB + Mongoose: Unique: true not working properly

I have a user.js model in my node app, and I'd like the username and a few other fields to be unique. In my model file I have properly declared the unique type seen below: // User Schema const UserSchema = new Schema({ // PERSONAL USER INFO …
jblew
  • 274
  • 1
  • 8
  • 21
8
votes
2 answers

Implementing pagination in vanilla GraphQL

Every tutorial I have found thus far has achieved pagination in GraphQL via Apollo, Relay, or some other magic framework. I was hoping to find answers in similar asked questions here but they don't exist. I understand how to setup the queries but…
NetOperator Wibby
  • 1,354
  • 5
  • 22
  • 44
8
votes
6 answers

Can't use "delete" operator on mongoose query results

I'm trying to delete a key before sending data back to the browser. For some reason, maybe because it is a mongoose object, this isn't working: delete myObject.property If I do console.log(delete myObject.property) I get true which I understand…
Glenn
  • 4,195
  • 9
  • 33
  • 41
8
votes
12 answers

Mongoose/MongoDb getting error geoNear is not a function

This is my controller file locations.js var mongoose = require('mongoose'); var Loc = mongoose.model('location'); module.exports.locationsListByDistance = function(req, res) { var lng = parseFloat(req.query.lng); var lat =…
Sid B
  • 83
  • 1
  • 7
8
votes
2 answers

How to get a list of available Mongoose Discriminators?

Given a situation where you have a User Scheme that you use to create a base model called User. And then for user roles, you use mongoose discriminators to create inherited models called Admin, Employee and Client. Is there a way to programmatically…