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

Mongoose findByIdAndUpdate not returning correct model

I have an issue I've not seen before with the Mongoose findByIdAndUpdate not returning the correct model in the callback. Here's the code: var id = args._id; var updateObj = {updatedDate: Date.now()}; _.extend(updateObj, args); …
JonRed
  • 2,853
  • 5
  • 28
  • 37
134
votes
35 answers

Mongoose Unique index not working!

I'm trying to let MongoDB detect a duplicate value based on its index. I think this is possible in MongoDB, but through the Mongoose wrapper things appear to be broken. So for something like this: User = new Schema ({ email: {type: String, index:…
foobar
  • 10,854
  • 18
  • 58
  • 66
133
votes
8 answers

Mongoose (mongodb) batch insert?

Does Mongoose v3.6+ support batch inserts now? I've searched for a few minutes but anything matching this query is a couple of years old and the answer was an unequivocal no. Edit: For future reference, the answer is to use Model.create(). create()…
Geuis
  • 41,122
  • 56
  • 157
  • 219
130
votes
14 answers

Return certain fields with .populate() from Mongoose

I'm getting returned a JSON value from MongoDB after I run my query. The problem is I do not want to return all the JSON associated with my return, I tried searching the docs and didn't find a proper way to do this. I was wondering what if it is at…
Sleep Deprived Bulbasaur
  • 2,368
  • 4
  • 21
  • 33
127
votes
24 answers

Mongoose the Typescript way...?

Trying to implement a Mongoose model in Typescript. Scouring the Google has revealed only a hybrid approach (combining JS and TS). How would one go about implementing the User class, on my rather naive approach, without the JS? Want to be able to…
Tim McNamara
  • 1,368
  • 2
  • 10
  • 11
127
votes
8 answers

MongoDB, remove object from array

Doc: { _id: 5150a1199fac0e6910000002, name: 'some name', items: [{ id: 23, name: 'item name 23' },{ id: 24, name: 'item name 24' }] } Is there a way to pull a specific object from an array? I.E. how do I pull…
lostintranslation
  • 23,756
  • 50
  • 159
  • 262
125
votes
12 answers

Mongoose: Get full list of users

I have tried to use Mongoose to send the list of all users as follows: server.get('/usersList', function(req, res) { var users = {}; User.find({}, function (err, user) { users[user._id] = user; }); res.send(users); }); Of…
Randomblue
  • 112,777
  • 145
  • 353
  • 547
124
votes
6 answers

JavaScript OOP in NodeJS: how?

I am used to the classical OOP as in Java. What are the best practices to do OOP in JavaScript using NodeJS? Each Class is a file with module.export? How to create Classes? this.Class = function() { //constructor? var privateField = "" …
fusio
  • 3,595
  • 6
  • 33
  • 47
124
votes
9 answers

Mongoose delete array element in document and save

I have an array in my model document. I would like to delete elements in that array based on a key I provide and then update MongoDB. Is this possible? Here's my attempt: var mongoose = require('mongoose'), Schema = mongoose.Schema; var…
occasl
  • 5,303
  • 5
  • 56
  • 81
123
votes
5 answers

How can you remove all documents from a collection with Mongoose?

I know how to... Remove a single document. Remove the collection itself. Remove all documents from the collection with Mongo. But I don't know how to remove all documents from the collection with Mongoose. I want to do this when the user clicks a…
Adam Zerner
  • 17,797
  • 15
  • 90
  • 156
122
votes
16 answers

MongoDB password with "@" in it

I'm trying to connect to a MongoDB database with a username and password using Mongoose in Node.js. All the docs say that the connection string should look like mongodb://username:password@host:port/db However, the password contains the '@'…
iZ.
  • 4,951
  • 5
  • 23
  • 16
122
votes
2 answers

Best way to perform a full text search in MongoDB and Mongoose

I'm searching on Google since days and I tried many things but I still can not perform a good full text search on my user collection. I tried ElasticSearch but was pretty impossible to query and paginate... I tried many plugins for Mongoose like…
Ayeye Brazo
  • 3,316
  • 7
  • 34
  • 67
120
votes
16 answers

ERR_HTTP_HEADERS_SENT: Cannot set headers after they are sent to the client

I'm facing this weird issue in NodeJS when using with Passport.js, Express and Mongoose. Basically, I get an error saying "Cannot set headers after they are sent to the client" even though I don't send more than one header. I've read other posts and…
lourdesr
  • 1,311
  • 2
  • 9
  • 5
118
votes
5 answers

log all queries that mongoose fire in the application

I have application using nodejs and mongodb. I have used mongoose for ODM. Now i want to log all the queries that mongoose fire during the whole application. How to log these?
codeofnode
  • 18,169
  • 29
  • 85
  • 142
118
votes
16 answers

Mongoose populate after save

I cannot manually or automatically populate the creator field on a newly saved object ... the only way I can find is to re-query for the objects I already have which I would hate to do. This is the setup: var userSchema = new mongoose.Schema({ …
Pykler
  • 14,565
  • 9
  • 41
  • 50