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
38126 questions
590
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…
skerit
  • 17,691
  • 22
  • 92
  • 138
570
votes
10 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
  • 12,476
  • 10
  • 32
  • 49
428
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
  • 24,425
  • 20
  • 100
  • 145
363
votes
6 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
  • 7,924
  • 8
  • 39
  • 73
315
votes
11 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
  • 6,942
  • 7
  • 37
  • 57
290
votes
7 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
  • 7,753
  • 6
  • 27
  • 38
275
votes
29 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
  • 4,822
  • 8
  • 37
  • 63
265
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
  • 11,498
  • 13
  • 55
  • 113
262
votes
6 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
  • 2,713
  • 2
  • 11
  • 8
260
votes
30 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
  • 8,803
  • 11
  • 35
  • 51
239
votes
8 answers

Push items into mongo array via mongoose

I've scoured SO a good bit looking for the answer but I'm sure that I'm lost for the right words to describe what I'm after. Basically I have a mongodb collection called 'people' The schema for that collection is as follows: people: { name:…
Neurax
  • 2,787
  • 2
  • 10
  • 17
235
votes
7 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
  • 2,793
  • 3
  • 13
  • 20
227
votes
8 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
  • 40,233
  • 16
  • 70
  • 84
199
votes
19 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
  • 59,357
  • 43
  • 187
  • 234
194
votes
8 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
  • 2,514
  • 3
  • 17
  • 16
1
2 3
99 100