Mongoose multiple connections

Mongoose handling connections via connections pool http://mongoosejs.com/docs/connections.html You can use server: {poolSize: 5} option for increase/decrease pool (number of parallel connections) If you need connections to different databases look here Mongoose and multiple database in single node.js project Example of multiple connections: var mongoose = require(‘mongoose’) var conn = mongoose.createConnection(‘mongodb://localhost/db1’); var conn2 = mongoose.createConnection(‘mongodb://localhost/db2’); var … Read more

$project in $lookup mongodb

You can use $lookup 3.6 syntax to $project the fields inside the $lookup pipeline User.aggregate([ { “$lookup”: { “from”: “schedules”, “let”: { “id”: “$_id.phone” }, “pipeline”: [ { “$match”: { “$expr”: { “$eq”: [“$customer.phone”, “$$id”] }}}, { “$project”: { “scheduleStart”: 1, “scheduleEnd”: 1 }} ], “as”: “user_detail” }} ])

Mongoose: extending schemas

Mongoose 3.8.1 now has support for Discriminators. A sample, from here: http://mongoosejs.com/docs/api.html#model_Model.discriminator function BaseSchema() { Schema.apply(this, arguments); this.add({ name: String, createdAt: Date }); } util.inherits(BaseSchema, Schema); var PersonSchema = new BaseSchema(); var BossSchema = new BaseSchema({ department: String }); var Person = mongoose.model(‘Person’, PersonSchema); var Boss = Person.discriminator(‘Boss’, BossSchema);

Mongoose – finding subdocuments by criteria

You can use $elemMatch as a query-projection operator in the most recent MongoDB versions. From the mongo shell: db.parents.find( {‘children.age’: {$gte: 18}}, {children:{$elemMatch:{age: {$gte: 18}}}}) This filters younger children’s documents out of the children array: { “_id” : …, “children” : [ { “name” : “Margaret”, “age” : 20 } ] } { “_id” : … Read more

Mongoose __v property – hide?

You can disable the “__v” attribute in your Schema definitions by setting the versionKey option to false. For example: var widgetSchema = new Schema({ … attributes … }, { versionKey: false }); I don’t think you can globally disable them, but can only do it per Schema. You can read more about Schema’s options here. … Read more

MongoDB via Mongoose JS – What is findByID?

findById is a convenience method on the model that’s provided by Mongoose to find a document by its _id. The documentation for it can be found here. Example: // Search by ObjectId var id = “56e6dd2eb4494ed008d595bd”; UserModel.findById(id, function (err, user) { … } ); Functionally, it’s the same as calling: UserModel.findOne({_id: id}, function (err, user) … Read more