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

mongoose - remove element in array using $pull

I have a node object that looks like this: { "_id":"58b336e105ac8eec70aef159", "name":"my node", "ip":"192.168.1.1", "__v":0, "configuration":{ "links":[ { "linkName":"athena_fw_listen_tcp_5555", …
elmiomar
  • 1,747
  • 2
  • 17
  • 27
8
votes
1 answer

Why mongoose findById return an error if ID is not found

Making an API with Node/Express + Mongo. I'm writing some unit test and I observed if I try to get /profile/1 for _id=1 (I let mongo put the ID by default so I cannot have _id=1) i got this error MongooseError: Cast to ObjectId failed for value "1"…
Ragnar
  • 2,550
  • 6
  • 36
  • 70
8
votes
2 answers

Specify an Index Name with Mongoose

When defining a Mongoose schema it is often prudent to specify what indexes should exist. That said, in many cases we would like to control the name of the index created, especially when those indexes are compound so they are understandable. …
Don Scott
  • 3,179
  • 1
  • 26
  • 40
8
votes
4 answers

how to connect another machine mongodb database inside local network ?

I follow this mongoose document enter link description here mongoose.connect('mongodb://localhost/waterDB'); Using This, I can connect local machine waterDBmongoDB DataBase My Personal Machine Local IP : 192.168.1.5 My Server Machine Local IP :…
Amila Sampath
  • 655
  • 4
  • 11
  • 28
8
votes
2 answers

Handling Mongoose Populated Fields in GraphQL

How do I represent a field that could be either a simple ObjectId string or a populated Object Entity? I have a Mongoose Schema that represents a 'Device type' as follows // assetSchema.js import * as mongoose from 'mongoose' const Schema =…
viztastic
  • 1,815
  • 1
  • 17
  • 17
8
votes
2 answers

Mongoose with ReplicaSet on Atlas

I have a replica set on MongoDB Atlas and this is my mongo shell connection string which connects perfectly: $ mongo…
Biruel Rick
  • 776
  • 1
  • 8
  • 17
8
votes
6 answers

Mongoose.js findOne returning query metadata

I am attemping to run a mongoose.findOne on my data base but I get unexpected results.My query is const User = mongoose.model('User', {name: String, email: String, passwordHash: String, validation: String, validationCode: String, favorites: Array…
Jack Tidbury
  • 183
  • 1
  • 1
  • 12
8
votes
2 answers

Storing site config as Mongoose model

I have website configuration (currently stored as JSON file), and I would like to move it to MongoDB and probably use Mongoose to handle read-write operations and perform validation through schemas. Configuration is an object with limited amount of…
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
8
votes
1 answer

Mongoose - Should I add refs to parent docs, child docs, or both?

I have two mongodb collections, let's call them parents and children. This parents/children design has a one-to-many relationship, and when I query the parent I want the children as well. It makes sense to have this model design: var ParentSchema =…
Alex Wohlbruck
  • 1,340
  • 2
  • 25
  • 41
8
votes
3 answers

Mongoose populate ObjectID from multiple possible collections

I have a mongoose model that looks something like this var LogSchema = new Schema({ item: { type: ObjectId, ref: 'article', index:true, }, }); But 'item' could be referenced from multiple collections. Is it possible…
Cade Embery
  • 433
  • 1
  • 5
  • 18
8
votes
2 answers

MongoDB throwing error Module not found: 'module'

I have a Mongo db setup on localhost:27017 and and trying to connect to it from my app that uses Webpack via Mongoose. I have Mongoose installed as a package. Here is my code: import mongoose from 'mongoose'; var db =…
red house 87
  • 1,837
  • 9
  • 50
  • 99
8
votes
1 answer

Mongodb $lookup dynamic collection

I have following schema where the item type might vary, and is mentioned in connections.kind. var userSchema = new Schema({ name: String, connections: [{ kind: String, item: { type: ObjectId, refPath: 'connections.kind' } }] }); var…
sidgate
  • 14,650
  • 11
  • 68
  • 119
8
votes
4 answers

How to register mongoose plugin for all schemas?

What am I missing? The Mongoose docs say that mongoose.plugin() registers a plugin for all schemas. This is not working. I CAN register my plugin on EACH schema. My plugin: module.exports = function (schema, options) { …
G. Deward
  • 1,542
  • 3
  • 17
  • 30
8
votes
0 answers

Using mongoose and mlab behind a proxy

Can't connect to my mlab database if I'm behind a proxy. Any idea how to achieve this? I've already googled and found no answer... Here is my connection: mongoose.connect(config.db.URI); var db = mongoose.connection; db.on('error', function(){ …
Ricardo Araújo
  • 207
  • 1
  • 12
8
votes
1 answer

Mongoose advanced custom schema object type

I couldn't find any example of an advanced custom schema type involving custom objects (or value-objects) in Mongoose >=4.4. Imagine that I want to use a custom type like: function Polygon(c) { this.bounds = [ /* some data */ ]; this.npoints =…
Dario
  • 3,905
  • 2
  • 13
  • 27