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
117
votes
21 answers

Mongoose Schema hasn't been registered for model

I'm using the MEAN stack and when I try to start the server using npm start, I get an exception saying that: schema hasn't been registered for model 'Post'. Use mongoose.model(name, schema) Here is my code inside /models/Posts.js: var mongoose =…
arash moeen
  • 4,533
  • 9
  • 40
  • 85
117
votes
8 answers

passport.js passport.initialize() middleware not in use

I am using node with express + mongoose and trying to use passport.js with restful api. I keep getting this exception after authentication success (I see the callback url on the…
Naor
  • 23,465
  • 48
  • 152
  • 268
117
votes
5 answers

Creating Multifield Indexes in Mongoose / MongoDB

I'm trying to find documentation, to no avail, on how to create multi-field indexes in Mongoosejs. In particular I have two fields that need to be indexed and unique. What is an example mongoose schema that indexes two fields together?
Dan
  • 3,389
  • 5
  • 34
  • 44
117
votes
17 answers

How to protect the password field in Mongoose/MongoDB so it won't return in a query when I populate collections?

Suppose I have two collections/schemas. One is the Users Schema with username and password fields, then, I have a Blogs Schema that has a reference to the Users Schema in the author field. If I use Mongoose to do something…
Luis Elizondo
  • 1,979
  • 2
  • 14
  • 15
117
votes
16 answers

How to drop a database with Mongoose?

I'm preparing a database creation script in Node.js and Mongoose. How can I check if the database already exists, and if so, drop (delete) it using Mongoose? I could not find a way to drop it with Mongoose.
Yaron Naveh
  • 23,560
  • 32
  • 103
  • 158
116
votes
11 answers

Does MongoDB's $in clause guarantee order

When using MongoDB's $in clause, does the order of the returned documents always correspond to the order of the array argument?
116
votes
9 answers

Which SchemaType in Mongoose is Best for Timestamp?

I'm using Mongoose, MongoDB, and Node. I would like to define a schema where one of its fields is a date\timestamp. I would like to use this field in order to return all of the records that have been updated in the last 5 minutes. Due to the fact…
Liatz
  • 4,997
  • 7
  • 28
  • 33
115
votes
1 answer

Combine two OR-queries with AND in Mongoose

I want to combine two OR-queries with AND in Monoose, like in this SQL statement: SELECT * FROM ... WHERE (a = 1 OR b = 1) AND (c=1 OR d=1) I tried this in a NodeJS module which only gets the model object from the main application: /********** Main…
Sonson123
  • 10,879
  • 12
  • 54
  • 72
114
votes
1 answer

(node:63208) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead

Where is this error coming from? I am not using ensureIndex or createIndex in my Nodejs application anywhere. I am using yarn package manager. Here is my code in index.js import express from 'express'; import path from 'path'; import bodyParser from…
aditya kumar
  • 2,905
  • 10
  • 39
  • 79
112
votes
4 answers

mongoose vs mongodb (nodejs modules/extensions), which better? and why?

I've just arrived to Node.js and see that there are many libs to use with the MongoDB, the most popular seem to be these two: (mongoose and mongodb). Can I get pros and cons of those extensions? Are there better alternatives to these two? Edit:…
norman784
  • 2,201
  • 3
  • 24
  • 31
112
votes
7 answers

Mongoose limit/offset and count query

Bit of an odd one on query performance... I need to run a query which does a total count of documents, and can also return a result set that can be limited and offset. So, I have 57 documents in total, and the user wants 10 documents offset by 20. I…
leepowell
  • 3,838
  • 8
  • 27
  • 35
112
votes
8 answers

Simplest way to wait some asynchronous tasks complete, in Javascript?

I want to drop some mongodb collections, but that's an asynchronous task. The code will be: var mongoose = require('mongoose'); mongoose.connect('mongo://localhost/xxx'); var conn =…
Freewind
  • 193,756
  • 157
  • 432
  • 708
111
votes
10 answers

Mongoose: deep population (populate a populated field)

I have Category model: Category: ... articles: [{type:ObjectId, ref:'Article'}] Article model contains ref to Account model. Article: ... account: {type:ObjectId, ref:'Account'} So, with populated articles Category model will…
f1nn
  • 6,989
  • 24
  • 69
  • 92
109
votes
3 answers

Why can't you modify the data returned by a Mongoose Query (ex: findById)

When I try to change any part of the data returned by a Mongoose Query it has no effect. I was trying to figure this out for about 2 hours yesterday, with all kinds of _.clone()s, using temporary storage variables, etc. Finally, just when I though I…
Toli
  • 5,547
  • 8
  • 36
  • 56
108
votes
18 answers

MongoDB: output 'id' instead of '_id'

I am using mongoose (node), what is the best way to output id instead of _id?
Johnny
  • 2,800
  • 6
  • 25
  • 33