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

How To Create Mongoose Schema with Array of Object IDs?

I have defined a mongoose user schema: var userSchema = mongoose.Schema({ email: { type: String, required: true, unique: true}, password: { type: String, required: true}, name: { first: { type: String, required: true, trim: true}, …
user2481095
  • 2,024
  • 7
  • 22
  • 32
105
votes
15 answers

Mongoose.js: Find user by username LIKE value

I like to to go find a user in mongoDb by looking for a user called value. The problem with: username: 'peter' is that i dont find it if the username is "Peter", or "PeTER".. or something like that. So i want to do like sql SELECT * FROM users…
techbech
  • 3,754
  • 5
  • 20
  • 28
104
votes
34 answers

Error Message: MongoError: bad auth Authentication failed through URI string

I'm trying to connect to my mongoDB server via the connection string given to me by mongo: "mongodb+srv://david:password@cluster0-re3gq.mongodb.net/test?retryWrites=true" In my code I am calling the connection through mongoose like this (obviously…
Djaenike
  • 1,645
  • 5
  • 21
  • 32
104
votes
5 answers

Mongoose - Create document if not exists, otherwise, update- return document in either case

I'm looking for a way to refactor part of my code to be shorter and simpler, but I don't know Mongoose very well and I'm not sure how to proceed. I am trying to check a collection for the existence of a document and, if it doesn't exist, create it.…
Connor
  • 1,815
  • 3
  • 17
  • 25
103
votes
13 answers

Uploading images using Node.js, Express, and Mongoose

Please consider newer answers that have more up-to-date information as things have changed over the years! Since many new Node.js libraries are quickly being rendered obsolete and there are relatively few examples anyways I want to ask about…
JohnAllen
  • 7,317
  • 9
  • 41
  • 65
102
votes
6 answers

Why does Mongoose have both schemas and models?

The two types of objects seem to be so close to one another that having both feels redundant. What is the point of having both schemas and models?
Randomblue
  • 112,777
  • 145
  • 353
  • 547
102
votes
14 answers

Node.js MongoDB ObjectId to string

IN Node.js, with MongoDB, Mongoosejs as orm I am doing this I have a model, User User.findOne({username:'someusername'}).exec(function(err,user){ console.log(user) //this gives full object with something like…
Swati Sharma
  • 1,025
  • 2
  • 7
  • 8
101
votes
13 answers

How can I save multiple documents concurrently in Mongoose/Node.js?

At the moment I use save to add a single document. Suppose I have an array of documents that I wish to store as single objects. Is there a way of adding them all with a single function call and then getting a single callback when it is done? I…
Hoa
  • 19,858
  • 28
  • 78
  • 107
100
votes
19 answers

Mongoose: CastError: Cast to ObjectId failed for value "[object Object]" at path "_id"

I am new to node.js, so I have a feeling that this will be something silly that I have overlooked, but I haven't been able to find an answer that fixes my problem. What I'm trying to do is create a path that will create a new child object, add it to…
jmblackmer
  • 1,217
  • 2
  • 8
  • 11
100
votes
6 answers

Querying after populate in Mongoose

I'm pretty new to Mongoose and MongoDB in general so I'm having a difficult time figuring out if something like this is possible: Item = new Schema({ id: Schema.ObjectId, dateCreated: { type: Date, default: Date.now }, title: { type:…
jschr
  • 1,866
  • 2
  • 16
  • 21
98
votes
10 answers

Convert Mongoose docs to json

I returned mongoose docs as json in this way: UserModel.find({}, function (err, users) { return res.end(JSON.stringify(users)); } However, user.__proto__ was also returned. How can I return without it? I tried this but not…
Trantor Liu
  • 8,770
  • 8
  • 44
  • 64
98
votes
9 answers

How do I get the objectID after I save an object in Mongoose?

var n = new Chat(); n.name = "chat room"; n.save(function(){ //console.log(THE OBJECT ID that I just saved); }); I want to console.log the object id of the object I just saved. How do I do that in Mongoose?
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
98
votes
7 answers

Mongoose populate with array of objects containing ref

I have a Mongoose schema with an array lists of objects that consist of a reference to another collection and a nested array of numbers: var Schema, exports, mongoose, schema; mongoose = require("mongoose"); Schema = mongoose.Schema; …
neverfox
  • 6,680
  • 7
  • 31
  • 40
93
votes
9 answers

DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead?

I am using mongoose findOneAndUpdate but still getting the error, DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead. But I am not even using findAndModify, why is it…
Sudhanshu Gaur
  • 7,486
  • 9
  • 47
  • 94
91
votes
8 answers

(node:3341) DeprecationWarning: Mongoose: mpromise

I'm trying to develop a class on the top of the mongoose with my custom methods, so I extended the mongoose with my own class but when I invoke to create a new car method it works but its strip and error, here I let you see what I'm trying to…
Audel O. Gutierrez
  • 1,031
  • 1
  • 8
  • 7