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
172
votes
8 answers

Why does mongoose always add an s to the end of my collection name

For example, this code results in a collection called "datas" being created var Dataset = mongoose.model('data', dataSchema); And this code results in a collection called "users" being created var User = mongoose.model('user', dataSchema); Thanks
Bob Ren
  • 2,139
  • 2
  • 17
  • 16
171
votes
6 answers

How to define object in array in Mongoose schema correctly with 2d geo index

I'm currently having problems in creating a schema for the document below. The response from the server always returns the "trk" field values as [Object]. Somehow I have no idea how this should work, as I tried at least all approaches which made…
niels_h
  • 1,869
  • 2
  • 13
  • 15
162
votes
30 answers

Server Discovery And Monitoring engine is deprecated

I am using Mongoose with my Node.js app and this is my configuration: mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, useFindAndModify: false }).then(()=>{ …
iLiA
  • 3,053
  • 4
  • 23
  • 45
161
votes
10 answers

Delete a key from a MongoDB document using Mongoose

I'm using the Mongoose Library for accessing MongoDB with node.js Is there a way to remove a key from a document? i.e. not just set the value to null, but remove it? User.findOne({}, function(err, user){ //correctly sets the key to null... but…
Daniel Beardsley
  • 19,907
  • 21
  • 66
  • 79
160
votes
4 answers

mongoDB/mongoose: unique if not null

I was wondering if there is way to force a unique collection entry but only if entry is not null. e Sample schema: var UsersSchema = new Schema({ name : {type: String, trim: true, index: true, required: true}, email : {type: String, trim:…
ezmilhouse
  • 8,933
  • 7
  • 29
  • 38
160
votes
9 answers

Mongoose -- Force collection name

I am trying to use mongoose to create a database and a collection in it. My code is: var mongoose = require('mongoose'); var db = mongoose.connect('mongodb://localhost/testdb'); var Schema = mongoose.Schema; var UserInfo = new Schema({ …
ravi
  • 1,601
  • 2
  • 11
  • 3
160
votes
3 answers

Mongoose's find method with $or condition does not work properly

Recently I start using MongoDB with Mongoose on Nodejs. When I use Model.find method with $or condition and _id field, Mongoose does not work properly. This does not work: User.find({ $or: [ { '_id': param }, { 'name': param }, {…
Younghan
  • 4,827
  • 3
  • 17
  • 9
154
votes
15 answers

Mongoose, update values in array of objects

Is there a way to update values in an object? { _id: 1, name: 'John Smith', items: [{ id: 1, name: 'item 1', value: 'one' },{ id: 2, name: 'item 2', value: 'two' }] } Lets say I want to update the name and…
lostintranslation
  • 23,756
  • 50
  • 159
  • 262
154
votes
6 answers

Mongoose subdocuments vs nested schema

I'm curious as to the pros and cons of using subdocuments vs a deeper layer in my main schema: var subDoc = new Schema({ name: String }); var mainDoc = new Schema({ names: [subDoc] }); or var mainDoc = new Schema({ names: [{ name:…
cyberwombat
  • 38,105
  • 35
  • 175
  • 251
151
votes
7 answers

How do you use Mongoose without defining a schema?

In previous versions of Mongoose (for node.js) there was an option to use it without defining a schema var collection = mongoose.noSchema(db, "User"); But in the current version the "noSchema" function has been removed. My schemas are likely to…
Christopher Tarquini
  • 11,176
  • 16
  • 55
  • 73
151
votes
5 answers

Referencing another schema in Mongoose

if I have two schemas like: var userSchema = new Schema({ twittername: String, twitterID: Number, displayName: String, profilePic: String, }); var User = mongoose.model('User') var postSchema = new Schema({ name: String, …
s_curry_s
  • 3,332
  • 9
  • 32
  • 47
149
votes
3 answers

Mongoose indexing in production code

Per the Mongoose documentation for MongooseJS and MongoDB/Node.js : When your application starts up, Mongoose automatically calls ensureIndex for each defined index in your schema. While nice for development, it is recommended this behavior be…
Nick S.
  • 2,203
  • 3
  • 19
  • 21
147
votes
9 answers

How to get all count of mongoose model?

How can I know the count of a model that data has been saved? there is a method of Model.count(), but it doesn't seem to work. var db = mongoose.connect('mongodb://localhost/myApp'); var userSchema = new…
smilexu
  • 1,473
  • 2
  • 9
  • 5
142
votes
7 answers

How do I limit the number of returned items?

myModel.find({}, function(err, items) { console.log(items.length); // Big number }); How can I limit the returned items to only the latest 10 items that were inserted?
Running Turtle
  • 12,360
  • 20
  • 55
  • 73
141
votes
6 answers

Mongoose query where value is not null

Looking to do the following query: Entrant .find enterDate : oneMonthAgo confirmed : true .where('pincode.length > 0') .exec (err,entrants)-> Am I doing the where clause properly? I want to select documents where pincode is…
wesbos
  • 25,839
  • 30
  • 106
  • 143