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
250
votes
22 answers

add created_at and updated_at fields to mongoose schemas

Is there a way to add created_at and updated_at fields to a mongoose schema, without having to pass them in everytime new MyModel() is called? The created_at field would be a date and only added when a document is created. The updated_at field would…
chovy
  • 72,281
  • 52
  • 227
  • 295
233
votes
21 answers

How to sort in mongoose?

I find no documentation for the sort modifier. The only insight is in the unit tests: spec.lib.query.js#L12 writer.limit(5).sort(['test', 1]).group('name') But it doesn't work for me: Post.find().sort(['updatedAt', 1]);
Philippe Rathé
  • 8,888
  • 3
  • 22
  • 13
217
votes
11 answers

In Mongoose, how do I sort by date? (node.js)

let's say I run this query in Mongoose: Room.find({}, (err,docs) => { }).sort({date:-1}); This doesn't work!
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
207
votes
5 answers

How can I generate an ObjectId with mongoose?

I'd like to generate a MongoDB ObjectId with Mongoose. Is there a way to access the ObjectId constructor from Mongoose? This question is about generating a new ObjectId from scratch. The generated ID is a brand new universally unique ID. Another…
Dmitry Minkovsky
  • 36,185
  • 26
  • 116
  • 160
200
votes
47 answers

Cannot overwrite model once compiled Mongoose

Not Sure what I'm doing wrong, here is my check.js var db = mongoose.createConnection('localhost', 'event-db'); db.on('error', console.error.bind(console, 'connection error:')); var a1= db.once('open',function(){ var user = mongoose.model('users',{…
Anathema.Imbued
  • 3,271
  • 4
  • 17
  • 18
200
votes
32 answers

What's Mongoose error Cast to ObjectId failed for value XXX at path "_id"?

When sending a request to /customers/41224d776a326fb40f000001 and a document with _id 41224d776a326fb40f000001 does not exist, doc is null and I'm returning a 404: Controller.prototype.show = function(id, res) { this.model.findById(id,…
gremo
  • 47,186
  • 75
  • 257
  • 421
200
votes
10 answers

MongoDB/Mongoose querying at a specific date?

Is it possible to query for a specific date ? I found in the mongo Cookbook that we can do it for a range Querying for a Date Range Like that : db.posts.find({"created_on": {"$gte": start, "$lt": end}}) But is it possible for a specific date ? This…
Unitech
  • 5,781
  • 5
  • 40
  • 47
198
votes
8 answers

Mongoose, Select a specific field with find

I'm trying to select only a specific field with exports.someValue = function(req, res, next) { //query with mongoose var query = dbSchemas.SomeValue.find({}).select('name'); query.exec(function (err, someValue) { if (err) return…
TlonXP
  • 3,325
  • 3
  • 26
  • 35
194
votes
16 answers

Populate nested array in mongoose

How can I populate "components" in the example document: { "__v": 1, "_id": "5252875356f64d6d28000001", "pages": [ { "__v": 1, "_id": "5252875a56f64d6d28000002", "page": { "components": [ …
Anton Shuvalov
  • 3,560
  • 3
  • 16
  • 20
193
votes
10 answers

Properly close mongoose's connection once you're done

I'm using mongoose in a script that is not meant to run continuously, and I'm facing what seems to be a very simple issue yet I can't find an answer; simply put once I make a call to any mongoose function that sends requests to mongodb my nodejs…
Lepidosteus
  • 11,779
  • 4
  • 39
  • 51
186
votes
35 answers

Failed to load c++ bson extension

A total node noob here. I've been trying to set up a sample node app but the following error keeps popping up every time I try to run: node app Failed to load c++ bson extension, using pure JS version events.js:72 throw er; // Unhandled…
Theja
  • 2,448
  • 3
  • 14
  • 18
182
votes
8 answers

Mongoose - What does the exec function do?

I came across a piece of Mongoose code that included a query findOne and then an exec() function. Ive never seen that method in Javascript before? What does it do exactly?
user439133
  • 1,943
  • 3
  • 12
  • 5
180
votes
7 answers

Mongoose and multiple database in single node.js project

I'm doing a Node.js project that contains sub projects. One sub project will have one Mongodb database and Mongoose will be use for wrapping and querying db. But the problem is Mongoose doesn't allow to use multiple databases in single mongoose…
pupot
  • 1,841
  • 2
  • 12
  • 7
175
votes
18 answers

mongoError: Topology was destroyed

I have a REST service built in node.js with Restify and Mongoose and a mongoDB with a collection with about 30.000 regular sized documents. I have my node service running through pmx and pm2. Yesterday, suddenly, node started crapping out errors…
dreagan
  • 2,426
  • 6
  • 24
  • 34
172
votes
9 answers

How to access a preexisting collection with Mongoose?

I have a large collection of 300 question objects in a database test. I can interact with this collection easily through MongoDB's interactive shell; however, when I try to get the collection through Mongoose in an express.js application I get an…
theabraham
  • 15,840
  • 9
  • 42
  • 41