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
748
votes
13 answers

Find MongoDB records where array field is not empty

All of my records have a field called "pictures". This field is an array of strings. I now want the newest 10 records where this array IS NOT empty. I've googled around, but strangely enough I haven't found much on this. I've read into the $where…
Jelle De Loecker
  • 20,999
  • 27
  • 100
  • 142
696
votes
13 answers

Find document with array that contains a specific value

If I have this schema... person = { name : String, favoriteFoods : Array } ... where the favoriteFoods array is populated with strings. How can I find all persons that have "sushi" as their favorite food using mongoose? I was hoping for…
Ludwig Magnusson
  • 13,964
  • 10
  • 38
  • 53
503
votes
23 answers

How do I update/upsert a document in Mongoose?

Perhaps it's the time, perhaps it's me drowning in sparse documentation and not being able to wrap my head around the concept of updating in Mongoose :) Here's the deal: I have a contact schema and model (shortened properties): var mongoose =…
Traveling Tech Guy
  • 27,194
  • 23
  • 111
  • 159
425
votes
8 answers

What is the "__v" field in Mongoose

I'm using Mongoose version 3 with MongoDB version 2.2. I've noticed a __v field has started appearing in my MongoDB documents. Is it something to do with versioning? How is it used?
Simon Lomax
  • 8,714
  • 8
  • 42
  • 75
422
votes
16 answers

Mongoose: findOneAndUpdate doesn't return updated document

Below is my code var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/test'); var Cat = mongoose.model('Cat', { name: String, age: {type: Number, default: 20}, create: {type: Date, default: Date.now}…
Dreams
  • 8,288
  • 10
  • 45
  • 71
358
votes
23 answers

E11000 duplicate key error index in mongodb mongoose

Following is my user schema in user.js model - var userSchema = new mongoose.Schema({ local: { name: { type: String }, email : { type: String, require: true, unique: true }, password: { type: String, require:true }, …
Trialcoder
  • 5,816
  • 9
  • 44
  • 66
355
votes
9 answers

mongodb/mongoose findMany - find all documents with IDs listed in array

I have an array of _ids and I want to get all docs accordingly, what's the best way to do it ? Something like ... // doesn't work ... of course ... model.find({ '_id' : [ '4ed3ede8844f0f351100000c', '4ed3f117a844e0471100000d', …
ezmilhouse
  • 8,933
  • 7
  • 29
  • 38
325
votes
12 answers

Push items into mongo array via mongoose

Basically I have a mongodb collection called 'people' whose schema is as follows: people: { name: String, friends: [{firstName: String, lastName: String}] } Now, I have a very basic express application that connects to…
Neurax
  • 3,657
  • 2
  • 13
  • 18
324
votes
7 answers

Stop Mongoose from creating _id property for sub-document array items

If you have subdocument arrays, Mongoose automatically creates ids for each one. Example: { _id: "mainId" subDocArray: [ { _id: "unwantedId", field: "value" }, { _id: "unwantedId", field:…
Atlas
  • 3,343
  • 2
  • 13
  • 9
319
votes
36 answers

How to paginate with Mongoose in Node.js?

I am writing a webapp with Node.js and mongoose. How can I paginate the results I get from a .find() call? I would like a functionality comparable to "LIMIT 50,100" in SQL.
Thomas
  • 10,289
  • 13
  • 39
  • 55
305
votes
9 answers

Comparing mongoose _id and strings

I have a node.js application that pulls some data and sticks it into an object, like this: var results = new Object(); User.findOne(query, function(err, u) { results.userId = u._id; } When I do an if/then based on that stored ID, the…
pat
  • 3,513
  • 3
  • 17
  • 20
280
votes
9 answers

How do you turn a Mongoose document into a plain object?

I have a document from a mongoose find that I want to extend before JSON encoding and sending out as a response. If I try adding properties to the doc it is ignored. The properties don't appear in Object.getOwnPropertyNames(doc) making a normal…
respectTheCode
  • 42,348
  • 18
  • 73
  • 86
278
votes
22 answers

Avoid "current URL string parser is deprecated" warning by setting useNewUrlParser to true

I have a database wrapper class that establishes a connection to some MongoDB instance: async connect(connectionString: string): Promise { this.client = await MongoClient.connect(connectionString) this.db =…
Lion
  • 16,606
  • 23
  • 86
  • 148
252
votes
11 answers

Difference between MongoDB and Mongoose

I wanted to use the mongodb database, but I noticed that there are two different databases with either their own website and installation methods: mongodb and mongoose. So I came up asking myself this question: "Which one do I use?". So in order to…
Ravindra Galav
  • 2,730
  • 2
  • 13
  • 16
250
votes
11 answers

Node.js Mongoose.js string to ObjectId function

Is there a function to turn a string into an objectId in node using mongoose? The schema specifies that something is an ObjectId, but when it is saved from a string, mongo tells me it is still just a string. The _id of the object, for instance, is…
JRPete
  • 3,074
  • 3
  • 19
  • 17
1
2 3
99 100