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
90
votes
3 answers

Project first item in an array to new field (MongoDB aggregation)

I am using Mongoose aggregation (MongoDB version 3.2). I have a field users which is an array. I want to $project first item in this array to a new field user. I tried { $project: { user: '$users[0]', otherField: 1 }}, { $project: { …
Hongbo Miao
  • 45,290
  • 60
  • 174
  • 267
88
votes
5 answers

MongoError: ns not found when try to drop collection

Mongoose throw an error i.e, "MongoError: ns not found" when i try to drop collection. Here is my mongoose code: var mongoose = require('bluebird').promisifyAll(require('mongoose')); ...... ...... ...... …
vineet
  • 13,832
  • 10
  • 56
  • 76
86
votes
5 answers

How do I query for distinct values in Mongoose?

I have a problem where I want to be able to get all the unique cities for a collection, and my code looks something like this: var mongoose = require("mongoose"), Schema = mongoose.Schema; var PersonSchema = new Schema({ name: String, …
Kit Sunde
  • 35,972
  • 25
  • 125
  • 179
86
votes
9 answers

how to use populate and aggregate in same statement?

This is my appointment collection: { _id: ObjectId("518ee0bc9be1909012000002"), date: ISODate("2013-05-13T22:00:00Z"), patient:ObjectId("518ee0bc9be1909012000002") } { _id: ObjectId("518ee0bc9be1909012000002"), date:…
Nour Sammour
  • 2,822
  • 1
  • 20
  • 19
84
votes
10 answers

How to exclude one particular field from a collection in Mongoose?

I have a NodeJS application with Mongoose ODM(Mongoose 3.3.1). I want to retrieve all fields except 1 from my collection.For Example: I have a collection Product Which have 6 fields,I want to select all except a field "Image" . I used "exclude"…
dany
  • 1,801
  • 7
  • 27
  • 40
83
votes
3 answers

How to return a complex JSON response with Node.js?

Using nodejs and express, I'd like to return one or multiple objects (array) using JSON. In the code below I output one JSON object at a time. It works but this isn't exactly what I want. The response produced isn't a valid JSON response since I…
Martin
  • 39,309
  • 62
  • 192
  • 278
83
votes
7 answers

Mongoose 'static' methods vs. 'instance' methods

I believe this question is similar to this one but the terminology is different. From the Mongoose 4 documentation: We may also define our own custom document instance methods too. // define a schema var animalSchema = new Schema({ name: String,…
Startec
  • 12,496
  • 23
  • 93
  • 160
83
votes
4 answers

Check mongoose connection state without creating new connection

I have some tests - namely Supertest - that load my Express app. This app creates a Mongoose connection. I would like to know how to check the status of that connection from within my test. In app.js mongoose.connect(...) In…
cyberwombat
  • 38,105
  • 35
  • 175
  • 251
83
votes
12 answers

Multiple populates - mongoosejs

Just a simple query, for example with a double ref in the model. Schema / Model var OrderSchema = new Schema({ user: { type : Schema.Types.ObjectId, ref : 'User', required: true }, meal: { type …
82
votes
1 answer

What is the difference between id and _id in mongoose?

What is the difference between _id and id in mongoose? Which is better for referencing?
Ari Porad
  • 2,834
  • 3
  • 33
  • 51
81
votes
6 answers

How to set ObjectId as a data type in mongoose

Using node.js, mongodb on mongoHQ and mongoose. I'm setting a schema for Categories. I would like to use the document ObjectId as my categoryId. var mongoose = require('mongoose'); var Schema = mongoose.Schema, ObjectId = Schema.ObjectId; var…
idophir
  • 14,451
  • 5
  • 24
  • 21
81
votes
3 answers

Can't find documents searching by ObjectId using Mongoose

Campaign.find {client_id:req.param('client_id')}, (error, campaigns) -> if error response = error: error.message else for campaign in campaigns query = campaign_id: campaign._id console.log…
Shamoon
  • 41,293
  • 91
  • 306
  • 570
80
votes
6 answers

Mongoose: how to define a combination of fields to be unique?

If I had a schema like this: var person = new Schema({ firstName: String, lastName: String, }); I'd like to ensure that there is only one document with the same firstName and lastName. How can I accomplish this?
martinpaulucci
  • 2,322
  • 5
  • 24
  • 28
80
votes
7 answers

Mongoose.js: remove collection or DB

Is it possible to remove collection or entire db using mongoose.js?
WHITECOLOR
  • 24,996
  • 37
  • 121
  • 181
79
votes
3 answers

Dealing with schema changes in Mongoose

What's the best practice (or tool) for updating/migrating Mongoose schemas as the application evolves?
mahemoff
  • 44,526
  • 36
  • 160
  • 222