mongoose-schema

Multer gridfs storage referencing uploaded file to a new collection

一笑奈何 提交于 2019-12-12 01:19:41
问题 Uploaded file using multer gridfs storage i want to reference it to newly created collection (movies). The fileID i referenced to the uploaded file is not the same with the uploaded file, even with that the movies collection is not saved on to the database. The fileID i referenced to the uploaded file is not the same with the uploaded file, even with that the movies collection is not saved on the database //movie schema const mongoose = require('mongoose'); const Schema = mongoose.Schema;

mongoose populate shows schema hasn't been registered for model populate

↘锁芯ラ 提交于 2019-12-11 17:53:02
问题 created mongoose schema called movie with property of object reference to another collection of created by multer-gridfs storage but when called populate method i got null on fileID The schema is const mongoose = require('mongoose'); const Schema = mongoose.Schema; const MovieSchema = new Schema({ description: String, category: String, token: String, fileID: { type: mongoose.Schema.Types.ObjectId, ref: 'fs' //created by multer gridfs storage } }); const Movie = mongoose.model('Movies',

How to restrict deep population in mongoose-autopopulate

北城以北 提交于 2019-12-11 17:18:04
问题 I am using mongoose-autopopulate npm package. In one query I want to deep populate and in another query I want to populate till one level. Here's my schema which is deep populating the data. ChildSchema = new Schema({ plugin_id: { type: String }, title: { type: String }, //card title text: { type: String }, is_valid: { type: Boolean,require:true, default: false}, children: [{ type: Schema.Types.ObjectId, ref: 'Child', autopopulate: true }] }); ChildSchema.plugin(autopopulate); module.exports

mongoose not returning a record

怎甘沉沦 提交于 2019-12-11 17:07:41
问题 Well, Last night I posted a question because I was frustrated getting mixed returns on a method call; I got an answer that worked, but I was left confused because it's what I was doing before the issue. That question is here: javascript undefined, when I just logged it and as you can tell, I WAS GETTING "KEY" BACK...it was just fluctuating between being an object and an array of objects. Tonight - I sit down to resume, and NOW I cant even get KEY back from the "findKey" call. (one step UP the

runValidators option not working on findByIDAndUpdate or findOneAndUpdate using mongoose

左心房为你撑大大i 提交于 2019-12-11 15:52:47
问题 I'm trying to validate an update to a document in mongoose. I have a validation check for a field as follows: reason: { type: String, enum: ['Finished', 'Incomplete'], required: function() { return this.status == 'Inactive' } } The field status is an enum as follows: status: { type: String, enum: ['Active', 'Inactive'], required: true } I have a findByIdAndUpdate call in an Express route which uses the option runValidators: true but if I update one of my documents such as {status: 'Inactive'

Mongoose update not working for me

笑着哭i 提交于 2019-12-11 15:04:47
问题 I have an endpoint where I'm trying to update a phone number in all "Contacts" documents after updating a user "Profile" document that shares their phone numbers. Based on the documents, I believe I have the right structure. The "Profiles" document updates fine, but "Contacts" does not. Here is the code: router.route('/updatephone/:id') // UPDATE PHONE .put(function(req, res){ Profile.findOne({'owner_id':req.params.id}, function(err, profile){ if(err) res.send(err); var phone = profile.phones

how to query collection which has multiple schemas or models?

人走茶凉 提交于 2019-12-11 14:10:57
问题 I inserted several documents that use different schemas(or models) in one collection and I need to query the collection through all documents. query is {reason: {$or: [1, 2]}} and {_id: objectid('xxx')}. how can i do? var eqAddPendingSchema = new Schema({ idString: String, name: String, category: String, life_cycle: Number, location: String, purchase_date: String, description: String, image: String, video: String, reason: Number, comment: String, created_at: Number, updated_at: Number },

Wildcard Text Index and inherited schemas

馋奶兔 提交于 2019-12-11 12:25:11
问题 I have 1 root schema and 4 inherited schemas using a discriminatorKey http://mongoosejs.com/docs/discriminators.html In this scenario I'm not clear if I can use a wild card text index. The purpose is to index all the string fields of each inherited schema independently. https://docs.mongodb.org/manual/core/index-text/ EDIT Adding this myRootSchema.index({ "_type": 1, "$**": "text" }); on my mongoose root schema I get this error when I try to query. Should I repeat this on the inherited

Insert default values not working mongodb

ぃ、小莉子 提交于 2019-12-11 07:26:56
问题 I am using mongoose version 5.2.5 and here is the sample model of my order .... let PlaceOrderSchema = new mongoose.Schema({ any: {} }, { strict: false }, { timestamps: { updatedAt: 'last_updated', createdAt: 'created' }); I am using the above model in main script with mongoose save and findOneAndUpdate . In our production system , we are seeing many document that does not have last_updated key missing in the save document. Here are sample code of the mongoose save method and findOneAndUpdate

How to access mongoose virtuals in express

会有一股神秘感。 提交于 2019-12-11 06:17:31
问题 I am trying to build a many to many relationship schema and trying to build a virtual to access the same. Finally trying to get the embedded objects when queried. My schema looks something like below //Mongoose Schema var item1 = new Schema ({ _id: Number, Item2Id: [{type: Number, ref: item2}], Detail1: String, Detail2: String ... },{toObject:{virtuals:true},toJSON:{virtuals:true}}); var item2 = new Schema ({ _id: Number, Item1Id: [{type: Number, ref: item1}], Detail1: String, Detail2: String