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
2 answers

Find documents whose array field contains at least n elements of a given array

It is basically what the title says. Input: myArray = an array of words I have an model that have field wordsCollection , which is an array field. How can I find all documents of that model whose wordsCollections has at least n elements of myArray
Mr Cold
  • 1,565
  • 2
  • 19
  • 29
8
votes
1 answer

Best way to structure my mongoose schema: embedded array , populate, subdocument?

Here is my current Schema Brand: var mongoose = require('mongoose'); var Schema = mongoose.Schema; var BrandSchema = new mongoose.Schema({ name: { type: String, lowercase: true , unique: true, required: true }, photo: { type: String ,…
NoobSter
  • 1,150
  • 1
  • 16
  • 39
8
votes
1 answer

Data models and business logic in isomorphic (React/Redux/Express/Mongo) app

I've recently built a few isomporphic/univeral projects using the React-Redux-Express-Mongoose stack. In my mongoose models is contained a lot of business-logic. As a very basic example (excuse my ES6): import mongoose, {Schema} from…
8
votes
1 answer

How to perform a $text search on a 'joined' collection via $lookup?

i’m new at Mongo, using v3.2. I have 2 collections Parent & Child. I’d like to use Parent.aggregate and use $lookup to “join” Child then perform $text $search on a field in Child and a date-range seach on the parent. Is this possible...?
8
votes
2 answers

How to set the value of a default attribute of a Mongoose schema based on a condition

I have this mongoose schema: var UserSchema = new Schema({ "name":String, "gender":String, }); I want to add another field named image. This image will have a default value if gender is male and it will have another default value if gender…
Lorenzo
  • 215
  • 4
  • 10
8
votes
1 answer

Getting large cryptic errors and warnings when trying to use mongoose with webpack

I'm using webpack to compile my es6 isomorphic react application into client and server bundles, respectively. I've installed mongoose and are attempting to use it in the server portion of my app, but I'm getting some horrendous errors in the…
ThinkingInBits
  • 10,792
  • 8
  • 57
  • 82
8
votes
1 answer

Does mongoose findOne on model return a promise?

I have a simple Node module that exports a function that makes a database query. The problem is that this function returns before the database query completes. "use strict"; var mongoose = require('mongoose'), Model =…
Matko
  • 3,386
  • 4
  • 21
  • 35
8
votes
1 answer

Mongoose populate across 2 databases

I have a node app that uses 2 databases. One is the the Names and the other for the rest of all the data. I have this connection setup: // DATABASE CONNECTION var APP_DB_URI = config.APP_DB; // mongodb://localhost:27017/app_db var app_DB =…
jofftiquez
  • 7,548
  • 10
  • 67
  • 121
8
votes
1 answer

Mongoose behaviour around Min and Max with Number

I have a Mongoose scheme like so... lightbox_opacity:{type:Number, min:0, max:1} I have 2 issues... When I try to insert a string "abc", it quietly ignores this fields insertion. Rest of the fields in the Schema get inserted successfully. I was…
Rahul Soni
  • 4,941
  • 3
  • 35
  • 58
8
votes
1 answer

Is there a way to index a document in mongoDB populating its references?

If I have these two collections: Book: { title: String, description: String author: {type: mongoose.Schema.Types.ObjectId, ref: 'User'}, } User: { _id: (generated by mongo) username: String } And I want to index Book for FTS. Can I do it in such…
Michael Seltenreich
  • 3,013
  • 2
  • 29
  • 55
8
votes
3 answers

Mongodb multi document insert ignore custom duplicate field error

I have to insert 3 recordset from array 1 already exists and 2 are new e.g: db.products.insert( [ { imagename: "pen1", qty: 21 }, { imagename: "pen", qty: 20 }, { imagename: "eraser", qty: 25 } ] ) Wherein "{ imagename: "pen",…
Rizwan Patel
  • 538
  • 2
  • 9
  • 27
8
votes
5 answers

Why doesn't MongoDB save my properties

I'm populating my DB with some dummy data where I'm trying to add a user. A user object is created but none of the properties are save... whats up? app.get('/setup', function (req, res) { User.findOne({ name: "Nick" }, function (err, user) { …
Jason94
  • 13,320
  • 37
  • 106
  • 184
8
votes
1 answer

mongoose where query with "or"

How would I do this in mongoose, I could not find anything with an "or" userModel.where('email') .equals(req.body.email) .or('username').equals(req.body.username) //need the "or" .exec(function (err, docs) { .... …
Michael W Riemer Jr
  • 531
  • 2
  • 8
  • 16
8
votes
3 answers

How to define varying attributes for a product system in mongoose (node.js)

I’m building a personal shop app where users can sell items to each other, but I’m having a difficult time figuring out how to manage the products. For instance if you want to sell a t-shirt you should be able to pick a size and color etc. but if…
chrs
  • 5,906
  • 10
  • 43
  • 74
8
votes
1 answer

Proper way to make a mongoose schema for a dynamic number of key/value pairs

I have a word counting function that returns an object with a set of key value pairs. Something like this: {a: 4, tale: 3, of: 8, two: 3, cities: 3 } Obviously this object can be of varying lengths and have different key/value pairs. I'd like to add…
Kenji Crosland
  • 2,914
  • 6
  • 31
  • 40