mongoose-schema

aggregate returns empty array - mongoose

跟風遠走 提交于 2019-12-11 03:25:56
问题 I have the following two simple queries: Comment.aggregate([{$match: { _id: req.params.id }}]) .exec(function(err, result) { // result is empty }); Comment.find({ _id: req.params.id }) .exec(function (err, result) { // correct result returned }); My problem is, that the aggregate-Function returns an empty array. Aren't they supposed to return the same result? 回答1: Yes, but you need to cast the id (which is a string) to an objectID :) let idToSearch = mongoose.Types.ObjectId(req.params.id)

Mongoose Populate not working with Array of ObjectIds

狂风中的少年 提交于 2019-12-11 03:02:34
问题 Here is my schema: /** Schemas */ var profile = Schema({ EmailAddress: String, FirstName: String, LastName: String, BusinessName: String }); var convSchema = Schema({ name: String, users: [{ type: Schema.Types.ObjectId, ref: 'Profiles' }], conversationType: { type: String, enum: ['single', 'group'], default: 'single' }, created: { type: Date, default: Date.now }, lastUpdated: { type: Date, default: Date.now } }); /** Models */ db.Profiles = mongoose.model('Profiles', profile); db

Error handling in async await

ぃ、小莉子 提交于 2019-12-10 17:44:21
问题 How can you implement error handling for Mongoose (current version v5.1.5)? For example, let's assume the following code where a user detail is being looked up. let u = await user.find({ code: id }).lean(); return u; And some error occurs, how should it be handled? Secondly, can we have centralised error handling function which will get triggered whenever an error happens in any of the Mongoose code, it gets directed to a particular function in the project where it can be handled. 回答1: You

How to get a list of available Mongoose Discriminators?

心不动则不痛 提交于 2019-12-10 02:54:14
问题 Given a situation where you have a User Scheme that you use to create a base model called User. And then for user roles, you use mongoose discriminators to create inherited models called Admin, Employee and Client. Is there a way to programmatically determine how many discriminations/inheritances/roles of the User model are available, as well as the available names? My question in terms of code: File : models/user.js var mongoose = require('mongoose'); var Schema = mongoose.Schema; var

Skip timestamps middleware for certain updates in Mongoose

。_饼干妹妹 提交于 2019-12-10 02:38:25
问题 My application uses Mongoose and has a schema that uses the timestamps option: var fooSchema = new Schema({ name: String, }, { timestamps: true, }); mongoose.model('Foo', fooSchema); So whenever an update is written to the collection, the updatedAt property is changed to the current date. But now I would like to add some changes that should not update the updatedAt property. Some answers on Stackoverflow (example) suggested to use Foo.collection as that allegedly accesses the native MongoDB

Mongoose: Filter collection based on values in another collection

扶醉桌前 提交于 2019-12-08 08:41:30
Consider documents that contain an arrays of ID of another collection, how can I find documents applying a filter based on the related collection using mongoose? I can't find my error Installation.aggregate( [ // Stage 1 { $lookup: { from: "users", localField: "_id", foreignField: "_id", as: "Users" } }, // Stage 2 { $unwind: { path: "$Users" } }, // Stage 3 { $match: {"Users.Email1" : "test@test.com"} }, { $sort: { _id: -1 } }, { $limit: 10 } ] ,function (err, installations) { console.log(installations); //<<<<< Empty /* Installation.populate( 'Parent', '_id Email1','Company'), function(err

update multiple elements in array mongodb [duplicate]

♀尐吖头ヾ 提交于 2019-12-08 08:34:27
This question already has answers here : How to Update Multiple Array Elements in mongodb (14 answers) Closed last year . I want to update chargeList isDelete property to true when date is greater than a specific value. my schema as follows. { "chargeList": [ { "date": ISODate("2013-06-26T18:57:30.012Z"), "price": "123", "isDelete": false }, { "date": ISODate("2013-06-27T18:57:30.012Z"), "price": "75", "isDelete": false } ] } schema is as follows. var ChargeListSchema= new Schema({ date: { type: Date , default: Date.now }, price: { type: String, required: false }, isDelete: { type: Boolean,

update multiple elements in array mongodb [duplicate]

半城伤御伤魂 提交于 2019-12-08 07:59:41
问题 This question already has answers here : How to Update Multiple Array Elements in mongodb (14 answers) Closed last year . I want to update chargeList isDelete property to true when date is greater than a specific value. my schema as follows. { "chargeList": [ { "date": ISODate("2013-06-26T18:57:30.012Z"), "price": "123", "isDelete": false }, { "date": ISODate("2013-06-27T18:57:30.012Z"), "price": "75", "isDelete": false } ] } schema is as follows. var ChargeListSchema= new Schema({ date: {

Are there ways to populate a schema in Joi from another schema (like Mongoose does)?

时光毁灭记忆、已成空白 提交于 2019-12-08 03:38:17
问题 I'm currently only using Joi to validate and building schemas. However I feel like I'm missing features like reference another schema like Mongoose does. Or is the only way to do this by using both (or mongoose only)? I want to use something similar to this: const mongoose = require('mongoose'); const Schema = mongoose.Schema; const personSchema = Schema({ _id: Schema.Types.ObjectId, name: String, age: Number, stories: [{ type: Schema.Types.ObjectId, ref: 'Story' }] }); const storySchema =

mongoose dynamic enum values

ぃ、小莉子 提交于 2019-12-08 02:12:31
问题 I have mongoose schema as below. 'use strict'; var mongoose = require('mongoose'), Schema = mongoose.Schema; var UserSchema = new Schema({ role: { type: String, enum: ['user', 'admin'] } }); mongoose.model('User', UserSchema); I want to set role enum values dynamically from database instead of hard coding, How can I do that? 回答1: Use "validate" on your field. E.G.: //My field is called "status" inside my schema. So "validate" will check if can pass or not. status: { type: String, default: