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
79
votes
6 answers

How to do raw mongodb operations in mongoose?

I'm asking this because when I write unit tests, I want to drop the test database and insert some initialize data, and also check the data in mongodb in testing. So I need raw operations to mongodb. How to do this in mongoose? What I can do now is…
Freewind
  • 193,756
  • 157
  • 432
  • 708
78
votes
6 answers

is there a mongoose connect error callback

how can i set a callback for the error handling if mongoose isn't able to connect to my DB? i know of connection.on('open', function () { ... }); but is there something like connection.on('error', function (err) { ... }); ?
Philipp Kyeck
  • 18,402
  • 15
  • 86
  • 123
78
votes
8 answers

How to get the latest and oldest record in mongoose.js (or just the timespan between them)

Basic problem I have a bunch of records and I need to get latest (most recent) and the oldest (least recent). When googling I found this topic where I saw a couple of queries: // option 1 Tweet.findOne({}, [], { $orderby : { 'created_at' : -1 } },…
askmike
  • 1,900
  • 4
  • 21
  • 27
77
votes
4 answers

mongoose save vs insert vs create

What are different ways to insert a document(record) into MongoDB using Mongoose? My current attempt: var mongoose = require('mongoose'); var Schema = mongoose.Schema; var notificationsSchema = mongoose.Schema({ "datetime" : { type:…
Maria Jane
  • 2,353
  • 6
  • 23
  • 39
77
votes
7 answers

Why Mongoose doesn't validate on update?

I have this code var ClientSchema = new Schema({ name: {type: String, required: true, trim: true} }); var Client = mongoose.model('Client', ClientSchema); Using express, I create a new client with this code var client = new…
Camilo
  • 2,844
  • 2
  • 29
  • 44
77
votes
4 answers

MongoDB via Mongoose JS - What is findByID?

I am writing a NodeJS server with ExpressJS, PassportJS, MongoDB and MongooseJS. I just managed to get PassportJS to use user data obtained via Mongoose to authenticate. But to make it work, I had to use a "findById" function like below. var…
Legendre
  • 3,108
  • 7
  • 31
  • 46
76
votes
11 answers

MongoDB - Query on the last element of an array?

I know that MongoDB supports the syntax find{array.0.field:"value"}, but I specifically want to do this for the last element in the array, which means I don't know the index. Is there some kind of operator for this, or am I out of luck? EDIT: To…
Joseph Blair
  • 1,385
  • 2
  • 12
  • 25
76
votes
8 answers

Mongoose __v property - hide?

Mongoose adds a '__v' property into Schema's for versioning - is it possible to disable this globally or globally hide it from all queries?
leepowell
  • 3,838
  • 8
  • 27
  • 35
76
votes
2 answers

Mongoose.js: how to implement create or update?

I have a request which body contains data and _id What is the better way to implement code that will update if record with _id exists and will create one is there is no one? My code: var obj = req.body; Model.findById(obj._id, function(err, data){ …
WHITECOLOR
  • 24,996
  • 37
  • 121
  • 181
75
votes
17 answers

MongooseError - Operation `users.findOne()` buffering timed out after 10000ms

My code was working before initially but I don't know why it just stopped working and gave me this error: MongooseError: Operation `users.findOne()` buffering timed out after 10000ms at Timeout.
yum
  • 1,125
  • 1
  • 8
  • 14
75
votes
5 answers

Mongoose - Why we make "mongoose.Promise = global.Promise" when setting a mongoose module?

I'm working with Mongoose. I have seen a lot of developers make the following command: mongoose.Promise = global.Promise; Then I was curious to see what is the original value of mongoose.Promise . I have entered in my editor the following command:…
Webwoman
  • 10,196
  • 12
  • 43
  • 87
74
votes
4 answers

Mongoose difference between .save() and using update()

To modify a field in an existing entry in mongoose, what is the difference between using model = new Model([...]) model.field = 'new value'; model.save(); and this Model.update({[...]}, {$set: {field: 'new value'}); The reason I'm asking this…
Edward Sun
  • 1,541
  • 3
  • 15
  • 26
72
votes
7 answers

What is the difference between findByIdAndRemove and findByIdAndDelete in mongoose?

The documentation here doesn't provide much of an explanation for why there are two different operations to accomplish the same thing, so I'm wondering what the differences are between them. Why might I choose to use one over the other?
Katie
  • 1,498
  • 1
  • 15
  • 33
72
votes
2 answers

Why should we use mongoose ODM instead of using directly Mongodb with mongodb driver for Nodejs/Express?

I have just started up with mongodb and I recently gone through Mongoose, an ODM for MongoDb. On the documentation, I couldn't find why we need to use Mongoose instead of using Mongodb directly except one reason which is we can define application…
codeofnode
  • 18,169
  • 29
  • 85
  • 142
71
votes
7 answers

How can I update multiple documents in mongoose?

I found the following script: Device.find(function(err, devices) { devices.forEach(function(device) { device.cid = ''; device.save(); }); }); MongoDB has the "multi" flag for an update over multiple documents but I wasn't able to get…
Philipp Kyeck
  • 18,402
  • 15
  • 86
  • 123